-
Notifications
You must be signed in to change notification settings - Fork 3
/
DocumentRangeCommandHandler.java
77 lines (70 loc) · 3.38 KB
/
DocumentRangeCommandHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.aql.ide.ui.handlers;
import java.util.Collections;
import org.eclipse.acceleo.aql.ide.ui.Activator;
import org.eclipse.acceleo.aql.ls.AcceleoLanguageServer;
import org.eclipse.acceleo.aql.ls.services.workspace.command.DocumentRangeParams;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageServers;
import org.eclipse.lsp4e.LanguageServers.LanguageServerDocumentExecutor;
import org.eclipse.lsp4j.ExecuteCommandParams;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.texteditor.ITextEditor;
/**
* Command handler for {@link DocumentRangeParams} send to the {@link AcceleoLanguageServer}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public abstract class DocumentRangeCommandHandler extends AbstractHandler {
/**
* Executes the command for the given {@link ExecutionEvent} and LSP command.
*
* @param event
* the {@link ExecutionEvent}
* @param lsCommand
* the LSP command
* @return the command for the given {@link ExecutionEvent} and LSP command
* @throws ExecutionException
*/
protected Object execute(ExecutionEvent event, String lsCommand) throws ExecutionException {
final ITextSelection selection = (ITextSelection)HandlerUtil.getCurrentSelection(event);
final ITextEditor editorPart = (ITextEditor)HandlerUtil.getActiveEditor(event);
try {
IDocument document = LSPEclipseUtils.getDocument(editorPart);
final TextDocumentIdentifier textIdentifier = LSPEclipseUtils.toTextDocumentIdentifier(document);
Position startPosition = LSPEclipseUtils.toPosition(selection.getOffset(), document);
Position endPosition = LSPEclipseUtils.toPosition(selection.getOffset() + selection.getLength(),
document);
final Range range = new Range(startPosition, endPosition);
final DocumentRangeParams documentRangeParams = new DocumentRangeParams(textIdentifier, range);
final LanguageServerDocumentExecutor executor = LanguageServers.forDocument(document);
final ExecuteCommandParams executionCommandParams = new ExecuteCommandParams(lsCommand,
Collections.singletonList(documentRangeParams));
executor.computeFirst(ls -> ls.getWorkspaceService().executeCommand(executionCommandParams));
} catch (BadLocationException e) {
Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID,
"unable to run LS command " + lsCommand, e));
}
return null;
}
}