为什么我有错误重叠范围是不允许的? [英] Why I have error overlapping range is not allowed?
本文介绍了为什么我有错误重叠范围是不允许的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建格式化程序扩展.不幸的是,我找不到很多如何处理简单任务的例子.
I am building formatter extension. Unfortunately I cannot find a lot of examples how to tackle simple tasks.
这是我的文件格式化程序提供程序,我尝试将一些关键字大写.这在扩展刷新后第一次有效,但第二次无效.
This is my files Formatter provider and I try to capitalize some keywords. This works first time after extension refresh but not second time.
我做错了什么?
'use strict';
import * as vscode from 'vscode';
export class STFormatterProvider implements vscode.DocumentFormattingEditProvider {
public out: Array<vscode.TextEdit> = [];
provideDocumentFormattingEdits(document: vscode.TextDocument) {
this.capitalize(document);
return this.out;
}
capitalize(document: vscode.TextDocument) {
let keywords = ['true', 'false', 'exit', 'continue', 'return', 'constant', 'retain'];
for (let line = 0; line < document.lineCount; line++) {
const element = document.lineAt(line);
let regEx = new RegExp(`\\b(${keywords.join('|')})\\b`, "ig");
let result = element.text.match(regEx);
if (result && result.length > 0) {
let str = element.text.replace(regEx, (match, content) => {
return match.toUpperCase();
});
this.out.push(
vscode.TextEdit.replace(element.range, str)
);
}
}
}
}
推荐答案
你有这个 overlap range
的主要原因是你没有重置 out
数组.
The main reason you have this overlap range
is that you don't reset the out
array.
我还进行了一些其他重构
I have also performed a few other refactors
class STFormatterProvider implements vscode.DocumentFormattingEditProvider {
public out: Array<vscode.TextEdit> = [];
provideDocumentFormattingEdits(document: vscode.TextDocument) {
this.out = [];
this.capitalize(document);
return this.out;
}
capitalize(document: vscode.TextDocument) {
let keywords = ['true', 'false', 'exit', 'continue', 'return', 'constant', 'retain'];
let regEx = new RegExp(`\\b(${keywords.join('|')})\\b`, "ig");
for (let lineNr = 0; lineNr < document.lineCount; ++lineNr) {
const line = document.lineAt(lineNr);
if (regEx.test(line.text)) {
let str = line.text.replace(regEx, (match) => { return match.toUpperCase(); });
this.out.push( vscode.TextEdit.replace(line.range, str) );
}
}
}
}
这篇关于为什么我有错误重叠范围是不允许的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文