Search in sources :

Example 71 with Range

use of org.eclipse.lsp4j.Range in project vscode-nextgenas by BowlerHatLLC.

the class LanguageServerUtils method getLocationFromSourceLocation.

/**
     * Converts a compiler source location to a language server location. May
     * return null if the line or column of the source location is -1.
     */
public static Location getLocationFromSourceLocation(ISourceLocation sourceLocation) {
    Path sourceLocationPath = Paths.get(sourceLocation.getSourcePath());
    Location location = new Location();
    location.setUri(sourceLocationPath.toUri().toString());
    Range range = getRangeFromSourceLocation(sourceLocation);
    if (range == null) {
        //this is probably generated by the compiler somehow
        return null;
    }
    location.setRange(range);
    return location;
}
Also used : Path(java.nio.file.Path) Range(org.eclipse.lsp4j.Range) ISourceLocation(org.apache.flex.compiler.common.ISourceLocation) Location(org.eclipse.lsp4j.Location)

Example 72 with Range

use of org.eclipse.lsp4j.Range in project sonarlint-core by SonarSource.

the class ServerMainTest method testCodeActionRuleDescription.

@Test
public void testCodeActionRuleDescription() throws Exception {
    String uri = getUri("foo.js");
    VersionedTextDocumentIdentifier docId = new VersionedTextDocumentIdentifier(1);
    docId.setUri(uri);
    lsProxy.getTextDocumentService().didChange(new DidChangeTextDocumentParams(docId, Collections.singletonList(new TextDocumentContentChangeEvent("function foo() {\n  alert('toto');\n}"))));
    List<? extends Command> codeActions = lsProxy.getTextDocumentService().codeAction(new CodeActionParams(new TextDocumentIdentifier(uri), new Range(new Position(1, 4), new Position(1, 4)), new CodeActionContext(waitForDiagnostics(uri)))).get();
    assertThat(codeActions).hasSize(1);
    String ruleKey = (String) codeActions.get(0).getArguments().get(0);
    assertThat(ruleKey).isEqualTo("javascript:S1442");
    lsProxy.getWorkspaceService().executeCommand(new ExecuteCommandParams(codeActions.get(0).getCommand(), codeActions.get(0).getArguments())).get();
    assertThat(client.ruleDescs).hasSize(1);
    assertThat(client.ruleDescs.get(0).getKey()).isEqualTo("javascript:S1442");
    assertThat(client.ruleDescs.get(0).getName()).contains("\"alert(...)\" should not be used");
    assertThat(client.ruleDescs.get(0).getHtmlDescription()).contains("can be useful for debugging during development");
    assertThat(client.ruleDescs.get(0).getType()).isEqualTo("VULNERABILITY");
    assertThat(client.ruleDescs.get(0).getSeverity()).isEqualTo("MINOR");
}
Also used : DidChangeTextDocumentParams(org.eclipse.lsp4j.DidChangeTextDocumentParams) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) VersionedTextDocumentIdentifier(org.eclipse.lsp4j.VersionedTextDocumentIdentifier) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) Position(org.eclipse.lsp4j.Position) ExecuteCommandParams(org.eclipse.lsp4j.ExecuteCommandParams) Range(org.eclipse.lsp4j.Range) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) Test(org.junit.Test)

Example 73 with Range

use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.

the class LanguageServerImpl method didChange.

@Override
public void didChange(final DidChangeTextDocumentParams params) {
    final Function0<BuildManager.Buildable> _function = () -> {
        final Function1<TextDocumentContentChangeEvent, TextEdit> _function_1 = (TextDocumentContentChangeEvent event) -> {
            Range _range = event.getRange();
            String _text = event.getText();
            return new TextEdit(_range, _text);
        };
        return this.workspaceManager.didChange(this._uriExtensions.toUri(params.getTextDocument().getUri()), params.getTextDocument().getVersion(), ListExtensions.<TextDocumentContentChangeEvent, TextEdit>map(params.getContentChanges(), _function_1));
    };
    final Function2<CancelIndicator, BuildManager.Buildable, List<IResourceDescription.Delta>> _function_1 = (CancelIndicator cancelIndicator, BuildManager.Buildable buildable) -> {
        return buildable.build(cancelIndicator);
    };
    this.requestManager.<BuildManager.Buildable, List<IResourceDescription.Delta>>runWrite(_function, _function_1);
}
Also used : IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) Function1(org.eclipse.xtext.xbase.lib.Functions.Function1) Range(org.eclipse.lsp4j.Range) TextDocumentContentChangeEvent(org.eclipse.lsp4j.TextDocumentContentChangeEvent) TextEdit(org.eclipse.lsp4j.TextEdit) BuildManager(org.eclipse.xtext.ide.server.BuildManager) ArrayList(java.util.ArrayList) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList) CancelIndicator(org.eclipse.xtext.util.CancelIndicator)

Example 74 with Range

use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.

the class ChangeConverter method _handleReplacements.

protected void _handleReplacements(final IEmfResourceChange change) {
    try {
        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            final URI uri = change.getResource().getURI();
            change.getResource().save(outputStream, null);
            byte[] _byteArray = outputStream.toByteArray();
            String _charset = this.getCharset(change.getResource());
            final String newContent = new String(_byteArray, _charset);
            final Function2<Document, XtextResource, List<TextEdit>> _function = (Document document, XtextResource resource) -> {
                List<TextEdit> _xblockexpression = null;
                {
                    Position _position = document.getPosition(0);
                    Position _position_1 = document.getPosition(document.getContents().length());
                    final Range range = new Range(_position, _position_1);
                    final TextEdit textEdit = new TextEdit(range, newContent);
                    _xblockexpression = this.addTextEdit(uri, textEdit);
                }
                return _xblockexpression;
            };
            this.workspaceManager.<List<TextEdit>>doRead(uri, _function);
        } finally {
            outputStream.close();
        }
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Position(org.eclipse.lsp4j.Position) TextEdit(org.eclipse.lsp4j.TextEdit) XtextResource(org.eclipse.xtext.resource.XtextResource) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.eclipse.xtext.ide.server.Document) Range(org.eclipse.lsp4j.Range) URI(org.eclipse.emf.common.util.URI)

Example 75 with Range

use of org.eclipse.lsp4j.Range in project xtext-core by eclipse.

the class ChangeConverter method _handleReplacements.

protected void _handleReplacements(final ITextDocumentChange change) {
    int _size = change.getReplacements().size();
    boolean _greaterThan = (_size > 0);
    if (_greaterThan) {
        final URI uri = change.getNewURI();
        final Function2<Document, XtextResource, List<TextEdit>> _function = (Document document, XtextResource resource) -> {
            List<TextEdit> _xblockexpression = null;
            {
                final Function1<ITextReplacement, TextEdit> _function_1 = (ITextReplacement replacement) -> {
                    TextEdit _xblockexpression_1 = null;
                    {
                        final Position start = document.getPosition(replacement.getOffset());
                        int _offset = replacement.getOffset();
                        int _length = replacement.getLength();
                        int _plus = (_offset + _length);
                        final Position end = document.getPosition(_plus);
                        final Range range = new Range(start, end);
                        String _replacementText = replacement.getReplacementText();
                        _xblockexpression_1 = new TextEdit(range, _replacementText);
                    }
                    return _xblockexpression_1;
                };
                final List<TextEdit> textEdits = ListExtensions.<ITextReplacement, TextEdit>map(change.getReplacements(), _function_1);
                _xblockexpression = this.addTextEdit(uri, ((TextEdit[]) Conversions.unwrapArray(textEdits, TextEdit.class)));
            }
            return _xblockexpression;
        };
        this.workspaceManager.<List<TextEdit>>doRead(uri, _function);
    }
}
Also used : Position(org.eclipse.lsp4j.Position) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) XtextResource(org.eclipse.xtext.resource.XtextResource) Document(org.eclipse.xtext.ide.server.Document) Range(org.eclipse.lsp4j.Range) URI(org.eclipse.emf.common.util.URI) TextEdit(org.eclipse.lsp4j.TextEdit) List(java.util.List)

Aggregations

Range (org.eclipse.lsp4j.Range)126 Position (org.eclipse.lsp4j.Position)59 Test (org.junit.Test)38 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)23 TextEdit (org.eclipse.lsp4j.TextEdit)23 ArrayList (java.util.ArrayList)22 List (java.util.List)17 Location (org.eclipse.lsp4j.Location)16 Document (org.eclipse.xtext.ide.server.Document)16 Diagnostic (org.eclipse.lsp4j.Diagnostic)15 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)13 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)13 PrepareRenameParams (org.eclipse.lsp4j.PrepareRenameParams)12 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)12 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)9 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)9 Command (org.eclipse.lsp4j.Command)9 CompletionList (org.eclipse.lsp4j.CompletionList)9 RenameParams (org.eclipse.lsp4j.RenameParams)9 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)8