Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to listen for code deleted by developers? I cannot obtain the deleted code using the following API, but I obtain the code after deleting it #885

Closed
Tracked by #922
zbsguilai opened this issue Jun 28, 2023 · 1 comment
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)

Comments

@zbsguilai
Copy link

zbsguilai commented Jun 28, 2023

vscode.workspace.onDidChangeTextDocument(async (event) => {
const document = event.document;
const contentChanges = event.contentChanges;

	for (const change of contentChanges) {
	  if (change.text === '') {
		await vscode.commands.executeCommand('workbench.action.files.save'); // 等待文档保存
  
		// 等待文档更新
		await new Promise((resolve) => setTimeout(resolve, 100));
  
		const deletedText = document.getText(change.range);
		console.log(`文件 ${document.fileName} 删除了文本:`);
		console.log(`被删除的文本内容:${deletedText}`);
		console.log(`范围:${change.range.start.line}:${change.range.start.character} - ${change.range.end.line}:${change.range.end.character}`);
	  }
	}
  })
)

Tasks

Preview Give feedback
No tasks being tracked yet.
@mjbvz mjbvz added the *question Issue represents a question, should be posted to StackOverflow (VS Code) label Oct 2, 2023
@vscodenpa
Copy link
Collaborator

Please ask your question on StackOverflow. We have a great community over there. They have already answered thousands of questions and are happy to answer yours as well. See also our issue reporting guidelines.

Happy Coding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
*question Issue represents a question, should be posted to StackOverflow (VS Code)
Projects
None yet
Development

No branches or pull requests

3 participants