Search in sources :

Example 1 with LineAndColumn

use of org.eclipse.xtext.util.LineAndColumn in project xtext-core by eclipse.

the class DocumentExtensions method newPosition.

public Position newPosition(Resource resource, int offset) {
    if (resource instanceof XtextResource) {
        ICompositeNode rootNode = ((XtextResource) resource).getParseResult().getRootNode();
        LineAndColumn lineAndColumn = getLineAndColumn(rootNode, offset);
        return new Position(lineAndColumn.getLine() - 1, lineAndColumn.getColumn() - 1);
    }
    return null;
}
Also used : Position(org.eclipse.lsp4j.Position) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) XtextResource(org.eclipse.xtext.resource.XtextResource) LineAndColumn(org.eclipse.xtext.util.LineAndColumn)

Example 2 with LineAndColumn

use of org.eclipse.xtext.util.LineAndColumn in project xtext-core by eclipse.

the class Bug437669Test method testUnresolved_02.

@Test
public void testUnresolved_02() {
    Type type = getContext();
    INode nameNode = NodeModelUtils.findNodesForFeature(type, ImportedURIPackage.Literals.TYPE__NAME).get(0);
    resolve(type, "BlaBlaBla", nameNode.getOffset(), nameNode.getLength());
    Resource resource = type.eResource();
    assertEquals(resource.getErrors().toString(), 1, resource.getErrors().size());
    LineAndColumn lineAndColumn = NodeModelUtils.getLineAndColumn(nameNode, nameNode.getOffset());
    Diagnostic diagnostic = (Diagnostic) resource.getErrors().get(0);
    assertEquals(nameNode.getOffset(), diagnostic.getOffset());
    assertEquals(nameNode.getLength(), diagnostic.getLength());
    assertEquals(lineAndColumn.getLine(), diagnostic.getLine());
    assertEquals(lineAndColumn.getColumn(), diagnostic.getColumn());
    assertEquals("Couldn't resolve reference to Type 'BlaBlaBla'.", diagnostic.getMessage());
}
Also used : Type(org.eclipse.xtext.linking.importedURI.Type) INode(org.eclipse.xtext.nodemodel.INode) Resource(org.eclipse.emf.ecore.resource.Resource) Diagnostic(org.eclipse.xtext.diagnostics.Diagnostic) LineAndColumn(org.eclipse.xtext.util.LineAndColumn) Test(org.junit.Test)

Example 3 with LineAndColumn

use of org.eclipse.xtext.util.LineAndColumn in project xtext-core by eclipse.

the class AbstractIdeQuickfixTest method quickfixesAreOffered.

private void quickfixesAreOffered(EObject target, String issueCode, String originalText, QuickfixExpectation... expected) {
    List<QuickfixExpectation> expectedSorted = IterableExtensions.sortBy(Arrays.asList(expected), it -> it.label);
    ICompositeNode elementNode = NodeModelUtils.getNode(target);
    LineAndColumn elementStartPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getOffset());
    LineAndColumn elementEndPosition = NodeModelUtils.getLineAndColumn(elementNode, elementNode.getEndOffset());
    Position startPos = new Position(elementStartPosition.getLine() - 1, elementStartPosition.getColumn() - 1);
    Position endPos = new Position(elementEndPosition.getLine() - 1, elementEndPosition.getColumn() - 1);
    Diagnostic issue = new Diagnostic();
    issue.setCode(issueCode);
    issue.setMessage("error");
    issue.setSeverity(DiagnosticSeverity.Error);
    issue.setSource("source");
    issue.setRange(new Range(startPos, endPos));
    ICodeActionService2.Options options = new ICodeActionService2.Options();
    options.setCancelIndicator(CancelIndicator.NullImpl);
    options.setDocument(new Document(Integer.valueOf(0), originalText));
    options.setResource((XtextResource) target.eResource());
    options.setLanguageServerAccess(new ILanguageServerAccess() {

        @Override
        public void addBuildListener(ILanguageServerAccess.IBuildListener listener) {
            throw new UnsupportedOperationException();
        }

        @Override
        public <T extends Object> CompletableFuture<T> doRead(String uri, Function<ILanguageServerAccess.Context, T> function) {
            ILanguageServerAccess.Context ctx = new ILanguageServerAccess.Context(options.getResource(), options.getDocument(), true, CancelIndicator.NullImpl);
            return CompletableFuture.completedFuture(function.apply(ctx));
        }

        @Override
        public <T extends Object> CompletableFuture<T> doReadIndex(Function<? super ILanguageServerAccess.IndexContext, ? extends T> function) {
            return null;
        }

        @Override
        public InitializeParams getInitializeParams() {
            return null;
        }

        @Override
        public InitializeResult getInitializeResult() {
            return null;
        }

        @Override
        public LanguageClient getLanguageClient() {
            return null;
        }

        @Override
        public ResourceSet newLiveScopeResourceSet(URI uri) {
            // re-using the existing ResourceSet because it contains the URI protocol mapping for "inmemory" resources.
            ResourceSet resourceSet = options.getResource().getResourceSet();
            return resourceSet;
        }
    });
    CodeActionParams codeActionParams = new CodeActionParams();
    codeActionParams.setRange(new Range(startPos, endPos));
    codeActionParams.setTextDocument(new TextDocumentIdentifier(target.eResource().getURI().toString()));
    CodeActionContext codeActionContext = new CodeActionContext();
    codeActionContext.setDiagnostics(Collections.singletonList(issue));
    codeActionParams.setContext(codeActionContext);
    options.setCodeActionParams(codeActionParams);
    List<DiagnosticResolution> actualIssueResolutions = IterableExtensions.sortBy(quickFixProvider.getResolutions(options, issue), DiagnosticResolution::getLabel);
    assertEquals("The number of quickfixes does not match!", expectedSorted.size(), actualIssueResolutions.size());
    for (int i = 0; i < actualIssueResolutions.size(); i++) {
        DiagnosticResolution actualIssueResolution = actualIssueResolutions.get(i);
        QuickfixExpectation expectedIssueResolution = expectedSorted.get(i);
        assertEquals(expectedIssueResolution.label, actualIssueResolution.getLabel());
        assertEquals(expectedIssueResolution.description, actualIssueResolution.getLabel());
        assertIssueResolutionResult(toUnixLineSeparator(expectedIssueResolution.getExpectedResult()), actualIssueResolution, originalText, options.getDocument());
    }
}
Also used : TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) DiagnosticResolution(org.eclipse.xtext.ide.editor.quickfix.DiagnosticResolution) Diagnostic(org.eclipse.lsp4j.Diagnostic) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) CompletableFuture(java.util.concurrent.CompletableFuture) LanguageClient(org.eclipse.lsp4j.services.LanguageClient) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) ILanguageServerAccess(org.eclipse.xtext.ide.server.ILanguageServerAccess) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Position(org.eclipse.lsp4j.Position) ICodeActionService2(org.eclipse.xtext.ide.server.codeActions.ICodeActionService2) InitializeParams(org.eclipse.lsp4j.InitializeParams) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LineAndColumn(org.eclipse.xtext.util.LineAndColumn) Range(org.eclipse.lsp4j.Range) InitializeResult(org.eclipse.lsp4j.InitializeResult) CodeActionContext(org.eclipse.lsp4j.CodeActionContext)

Example 4 with LineAndColumn

use of org.eclipse.xtext.util.LineAndColumn in project xtext-core by eclipse.

the class LineAndColumnTest method doAssertLineAndColumn.

protected void doAssertLineAndColumn(String text, int offset, int line, int column) {
    int[] lineBreaks = AccessibleNodeModelUtils.computeLineBreaks(text);
    LineAndColumn lineAndColumn = AccessibleNodeModelUtils.getLineAndColumn(text, lineBreaks, offset);
    Assert.assertEquals("line", line, lineAndColumn.getLine());
    Assert.assertEquals("column", column, lineAndColumn.getColumn());
    LineAndColumn offsetZero = AccessibleNodeModelUtils.getLineAndColumn(text, lineBreaks, 0);
    Assert.assertEquals("line", 1, offsetZero.getLine());
    Assert.assertEquals("column", 1, offsetZero.getColumn());
}
Also used : LineAndColumn(org.eclipse.xtext.util.LineAndColumn)

Example 5 with LineAndColumn

use of org.eclipse.xtext.util.LineAndColumn in project xtext-core by eclipse.

the class DiagnosticConverterImpl method getLocationForNode.

/**
 * Computes {@link IssueLocation} for the given offset and length in the given node.
 *
 * @since 2.21
 */
protected IssueLocation getLocationForNode(INode node, int offset, int length) {
    IssueLocation result = new IssueLocation();
    result.offset = offset;
    result.length = length;
    LineAndColumn lineAndColumnStart = NodeModelUtils.getLineAndColumn(node, offset);
    result.lineNumber = lineAndColumnStart.getLine();
    result.column = lineAndColumnStart.getColumn();
    LineAndColumn lineAndColumnEnd = NodeModelUtils.getLineAndColumn(node, offset + length);
    result.lineNumberEnd = lineAndColumnEnd.getLine();
    result.columnEnd = lineAndColumnEnd.getColumn();
    return result;
}
Also used : LineAndColumn(org.eclipse.xtext.util.LineAndColumn)

Aggregations

LineAndColumn (org.eclipse.xtext.util.LineAndColumn)7 Position (org.eclipse.lsp4j.Position)2 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)2 CompletableFuture (java.util.concurrent.CompletableFuture)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 CodeActionContext (org.eclipse.lsp4j.CodeActionContext)1 CodeActionParams (org.eclipse.lsp4j.CodeActionParams)1 Diagnostic (org.eclipse.lsp4j.Diagnostic)1 InitializeParams (org.eclipse.lsp4j.InitializeParams)1 InitializeResult (org.eclipse.lsp4j.InitializeResult)1 Range (org.eclipse.lsp4j.Range)1 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)1 LanguageClient (org.eclipse.lsp4j.services.LanguageClient)1 Diagnostic (org.eclipse.xtext.diagnostics.Diagnostic)1 DiagnosticResolution (org.eclipse.xtext.ide.editor.quickfix.DiagnosticResolution)1 Document (org.eclipse.xtext.ide.server.Document)1 ILanguageServerAccess (org.eclipse.xtext.ide.server.ILanguageServerAccess)1