UNPKG

sprotty-vscode

Version:

Glue code to integrate Sprotty diagrams in VSCode extensions (extension part)

85 lines 3.73 kB
"use strict"; /******************************************************************************** * Copyright (c) 2020-2023 TypeFox and others. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0 which is available at * http://www.eclipse.org/legal/epl-2.0. * * This Source Code may also be made available under the following Secondary * Licenses when the conditions for such availability set forth in the Eclipse * Public License v. 2.0 are satisfied: GNU General Public License, version 2 * with the GNU Classpath Exception which is available at * https://www.gnu.org/software/classpath/license.html. * * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 ********************************************************************************/ Object.defineProperty(exports, "__esModule", { value: true }); exports.addLspLabelEditActionHandler = void 0; const editing_1 = require("sprotty-vscode-protocol/lib/lsp/editing"); const vscode = require("vscode"); const node_1 = require("vscode-languageclient/node"); const lsp_utils_1 = require("../lsp-utils"); function addLspLabelEditActionHandler(endpoint) { const handler = async (action) => { switch (action.editKind) { case 'xref': return chooseCrossReference(action, endpoint); case 'name': return renameElement(action, endpoint); } }; endpoint.addActionHandler(editing_1.LspLabelEditAction.KIND, handler); } exports.addLspLabelEditActionHandler = addLspLabelEditActionHandler; ; async function chooseCrossReference(action, endpoint) { const completions = await endpoint.languageClient.sendRequest(node_1.CompletionRequest.type, { textDocument: { uri: action.location.uri }, position: (0, lsp_utils_1.convertPosition)(action.location.range.start) }); if (!completions) { return; } const completionItems = Array.isArray(completions) ? completions : completions.items; const quickPickItems = filterCompletionItems(completionItems) .map(completionItem => ({ label: completionItem.textEdit.newText, value: completionItem })); const pick = await vscode.window.showQuickPick(quickPickItems); if (pick && pick.value.textEdit) { const workspaceEdit = new vscode.WorkspaceEdit(); const textEdit = vscode.TextEdit.replace((0, lsp_utils_1.convertRange)(action.location.range), pick.value.textEdit.newText); workspaceEdit.set((0, lsp_utils_1.convertUri)(action.location.uri), [textEdit]); await vscode.workspace.applyEdit(workspaceEdit); } } function filterCompletionItems(items) { return items.filter(item => item.kind === node_1.CompletionItemKind.Reference); } async function renameElement(action, endpoint) { const canRename = await endpoint.languageClient.sendRequest(node_1.PrepareRenameRequest.type, { textDocument: { uri: action.location.uri }, position: action.location.range.start }); if (!canRename) { return; } const newName = await vscode.window.showInputBox({ prompt: 'Enter new name', placeHolder: 'new name', value: action.initialText }); if (newName) { const workspaceEdit = await endpoint.languageClient.sendRequest(node_1.RenameRequest.type, { textDocument: { uri: action.location.uri }, position: action.location.range.start, newName }); if (workspaceEdit) { await vscode.workspace.applyEdit((0, lsp_utils_1.convertWorkspaceEdit)(workspaceEdit)); } } } //# sourceMappingURL=lsp-label-edit-action-handler.js.map