Polish extension
This commit is contained in:
@@ -3,6 +3,10 @@ import { getTargetGuideMardown } from "./utils";
|
||||
import { CLI } from "./extension";
|
||||
import { createConverter } from "vscode-languageclient/lib/common/protocolConverter";
|
||||
import fs from "fs";
|
||||
import { CANCELLED } from "./cli";
|
||||
|
||||
const APPLY = "Apply";
|
||||
const PREVIEW = "Preview"
|
||||
|
||||
const CONVERTER = createConverter(undefined, true, true);
|
||||
export async function handleGuideApply(uri: Uri) {
|
||||
@@ -12,23 +16,41 @@ export async function handleGuideApply(uri: Uri) {
|
||||
return CLI.guideApply(uri);
|
||||
}
|
||||
|
||||
export async function handleGuideRun(uri: Uri) {
|
||||
if (!uri) {
|
||||
uri = await getTargetGuideMardown();
|
||||
}
|
||||
const lspEdit = await CLI.guideLspEdit(uri);
|
||||
const workspaceEdit = await CONVERTER.asWorkspaceEdit(lspEdit);
|
||||
|
||||
// This is some sort of workaround for undo
|
||||
// If editors for existing doc not opened then undo isn't properly working
|
||||
await Promise.all(workspaceEdit.entries().map(async ([uri, edits]) => {
|
||||
if (fs.existsSync(uri.fsPath)) {
|
||||
const doc = await workspace.openTextDocument(uri.fsPath);
|
||||
await window.showTextDocument(doc);
|
||||
export async function handleGuideApplyWorkspaceEdit(uri: Uri) {
|
||||
try {
|
||||
if (!uri) {
|
||||
uri = await getTargetGuideMardown();
|
||||
}
|
||||
}));
|
||||
const lspEdit = await CLI.guideLspEdit(uri);
|
||||
|
||||
if (lspEdit.changeAnnotations && Object.keys(lspEdit.changeAnnotations).length > 0) {
|
||||
const answer = await window.showInformationMessage("Do you want to apply changes right away or preview before applying?", APPLY, PREVIEW);
|
||||
if (!answer) {
|
||||
return;
|
||||
}
|
||||
const preview = answer === PREVIEW;
|
||||
Object.keys(lspEdit.changeAnnotations).forEach(changeId => {
|
||||
lspEdit.changeAnnotations[changeId].needsConfirmation = preview;
|
||||
});
|
||||
}
|
||||
const workspaceEdit = await CONVERTER.asWorkspaceEdit(lspEdit);
|
||||
|
||||
return await workspace.applyEdit(workspaceEdit, {
|
||||
isRefactoring: true
|
||||
});
|
||||
// This is some sort of workaround for undo
|
||||
// If editors for existing doc not opened then undo isn't properly working
|
||||
await Promise.all(workspaceEdit.entries().map(async ([uri, edits]) => {
|
||||
if (fs.existsSync(uri.fsPath)) {
|
||||
const doc = await workspace.openTextDocument(uri.fsPath);
|
||||
await window.showTextDocument(doc);
|
||||
}
|
||||
}));
|
||||
|
||||
return await workspace.applyEdit(workspaceEdit, {
|
||||
isRefactoring: true
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
if (error !== CANCELLED) {
|
||||
window.showErrorMessage(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user