TextEditorEdit.replace是async,代码执行完了怎么执行?
TextEditorEdit.replace is async, how to execute code when it is finished?
当我开发 this VS Code extension 时,我不得不将光标移动到我用 vscode.window.activeTextEditor.edit
方法创建的一行,但后来我意识到当我尝试时该行不存在移动光标,所以我不得不放一个 setTimeout
来尝试移动光标:
let editor: TextEditor = vscode.window.activeTextEditor;
let selections: Selection[] = editor.selections;
let doc: TextDocument = editor.document;
editor.edit(function (edit: TextEditorEdit): void {
selections.forEach((selection: Selection, index: number) => {
for (let i = selection.start.line; i <= selection.end.line; i++) {
let selLine: TextLine = doc.lineAt(i);
let insertPos: Range = selLine.range;
let insertLineText: string = selLine.text;
// This is async :(
edit.replace(insertPos, insertSemicolon(insertLineText, newLine));
}
});
if (newLine) {
// Move cursor to the next line
setTimeout(() => {
vscode.commands.executeCommand("cursorMove", {
to: "down",
by: "wrappedLine",
select: false,
value: 1
}).then(() => {
vscode.commands.executeCommand("cursorMove", {
to: "wrappedLineEnd",
by: "wrappedLine",
select: false
})
});
}, 50);
}
});
据我所知,这不是一个好的做法,因为我不能确定代码执行时该行是否存在,我试图找到一种方法来仅在编辑完成时执行此代码完成替换所有内容。
您需要将光标操作移到编辑块之外。 TextEditor.edit
中的回调构建了一组编辑,然后异步应用这些编辑。 edit
returns Thenable<boolean>
指示编辑是否成功完成。
试试这样的东西:
let editor: TextEditor = vscode.window.activeTextEditor;
let selections: Selection[] = editor.selections;
let doc: TextDocument = editor.document;
editor.edit(function (edit: TextEditorEdit): void {
selections.forEach((selection: Selection, index: number) => {
for (let i = selection.start.line; i <= selection.end.line; i++) {
let selLine: TextLine = doc.lineAt(i);
let insertPos: Range = selLine.range;
let insertLineText: string = selLine.text;
edit.replace(insertPos, insertSemicolon(insertLineText, newLine));
}
});
}).then(success => {
if (!success) {
return
}
if (newLine) {
// Move cursor to the next line
...
}
})
当我开发 this VS Code extension 时,我不得不将光标移动到我用 vscode.window.activeTextEditor.edit
方法创建的一行,但后来我意识到当我尝试时该行不存在移动光标,所以我不得不放一个 setTimeout
来尝试移动光标:
let editor: TextEditor = vscode.window.activeTextEditor;
let selections: Selection[] = editor.selections;
let doc: TextDocument = editor.document;
editor.edit(function (edit: TextEditorEdit): void {
selections.forEach((selection: Selection, index: number) => {
for (let i = selection.start.line; i <= selection.end.line; i++) {
let selLine: TextLine = doc.lineAt(i);
let insertPos: Range = selLine.range;
let insertLineText: string = selLine.text;
// This is async :(
edit.replace(insertPos, insertSemicolon(insertLineText, newLine));
}
});
if (newLine) {
// Move cursor to the next line
setTimeout(() => {
vscode.commands.executeCommand("cursorMove", {
to: "down",
by: "wrappedLine",
select: false,
value: 1
}).then(() => {
vscode.commands.executeCommand("cursorMove", {
to: "wrappedLineEnd",
by: "wrappedLine",
select: false
})
});
}, 50);
}
});
据我所知,这不是一个好的做法,因为我不能确定代码执行时该行是否存在,我试图找到一种方法来仅在编辑完成时执行此代码完成替换所有内容。
您需要将光标操作移到编辑块之外。 TextEditor.edit
中的回调构建了一组编辑,然后异步应用这些编辑。 edit
returns Thenable<boolean>
指示编辑是否成功完成。
试试这样的东西:
let editor: TextEditor = vscode.window.activeTextEditor;
let selections: Selection[] = editor.selections;
let doc: TextDocument = editor.document;
editor.edit(function (edit: TextEditorEdit): void {
selections.forEach((selection: Selection, index: number) => {
for (let i = selection.start.line; i <= selection.end.line; i++) {
let selLine: TextLine = doc.lineAt(i);
let insertPos: Range = selLine.range;
let insertLineText: string = selLine.text;
edit.replace(insertPos, insertSemicolon(insertLineText, newLine));
}
});
}).then(success => {
if (!success) {
return
}
if (newLine) {
// Move cursor to the next line
...
}
})