gpt4 book ai didi

visual-studio-code - 是否可以在 VSCode 中以编程方式将文本文档设置为脏?

转载 作者:行者123 更新时间:2023-12-04 06:38:47 24 4
gpt4 key购买 nike

是否可以设置 TextDocument在 VSCode 中以编程方式脏吗?就像是

openedDocument.setDirty()

最佳答案

没有直接的方法可以做到这一点。 TextDocument.isDirty 是只读属性。

但是,我整理了一个设置 isDirty 的解决方法。通过进行无效的编辑(使用 VSCode 1.37.1 测试):

// Set the dirty bit on 'textEditor'.  This is meant to be called as a
// text editor command.
async function setDirty(textEditor: TextEditor, editBuilder: TextEditorEdit)
: Promise<void>
{
// The strategy here is to make a change that has no effect. If the
// document has text in it, we can replace some text with itself
// (simply inserting an empty string does not work). We prefer to
// edit text at the end of the file in order to minimize spurious
// recomputation by analyzers.

// Try to replace the last line.
if (textEditor.document.lineCount >= 2) {
const lineNumber = textEditor.document.lineCount-2;
const lastLineRange = new Range(
new Position(lineNumber, 0),
new Position(lineNumber+1, 0));
const lastLineText = textEditor.document.getText(lastLineRange);
editBuilder.replace(lastLineRange, lastLineText);
return;
}

// Try to replace the first character.
const range = new Range(new Position(0, 0), new Position(0, 1));
const text = textEditor.document.getText(range);
if (text.length > 0) {
editBuilder.replace(range, text);
return;
}

// With an empty file, we first add a character and then remove it.
// This has to be done as two edits, which can cause the cursor to
// visibly move and then return, but we can at least combine them
// into a single undo step.
await textEditor.edit(
(innerEditBuilder: TextEditorEdit) => {
innerEditBuilder.replace(range, " ");
},
{ undoStopBefore: true, undoStopAfter: false });

await textEditor.edit(
(innerEditBuilder: TextEditorEdit) => {
innerEditBuilder.replace(range, "");
},
{ undoStopBefore: false, undoStopAfter: true });
}

在您的 activate功能,将它与类似的东西连接起来:

  context.subscriptions.push(
commands.registerTextEditorCommand("extension.setDirty", setDirty));

关于visual-studio-code - 是否可以在 VSCode 中以编程方式将文本文档设置为脏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49991815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com