Search in sources :

Example 6 with Document

use of org.eclipse.xtext.ide.server.Document in project xtext-core by eclipse.

the class WorkspaceManager method doRead.

public <T extends Object> T doRead(final URI uri, final Function2<? super Document, ? super XtextResource, ? extends T> work) {
    final URI resourceURI = uri.trimFragment();
    final ProjectManager projectMnr = this.getProjectManager(resourceURI);
    Resource _resource = null;
    if (projectMnr != null) {
        _resource = projectMnr.getResource(resourceURI);
    }
    final XtextResource resource = ((XtextResource) _resource);
    if ((resource == null)) {
        return work.apply(null, null);
    }
    Document doc = this.getDocument(resource);
    Resource _resource_1 = projectMnr.getResource(resourceURI);
    return work.apply(doc, ((XtextResource) _resource_1));
}
Also used : XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) ProjectManager(org.eclipse.xtext.ide.server.ProjectManager)

Example 7 with Document

use of org.eclipse.xtext.ide.server.Document in project xtext-core by eclipse.

the class DocumentTest method testUpdate_02.

@Test
public void testUpdate_02() {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("hello world");
    _builder.newLine();
    _builder.append("foo");
    _builder.newLine();
    _builder.append("bar");
    _builder.newLine();
    String _normalize = this.normalize(_builder);
    Document _document = new Document(1, _normalize);
    final Procedure1<Document> _function = (Document it) -> {
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("hello world");
        _builder_1.newLine();
        _builder_1.append("future");
        _builder_1.newLine();
        _builder_1.append("bar");
        _builder_1.newLine();
        TextEdit _change = this.change(this.position(1, 1), this.position(1, 3), "uture");
        Assert.assertEquals(this.normalize(_builder_1), it.applyChanges(Collections.<TextEdit>unmodifiableList(CollectionLiterals.<TextEdit>newArrayList(_change))).getContents());
    };
    ObjectExtensions.<Document>operator_doubleArrow(_document, _function);
}
Also used : TextEdit(org.eclipse.lsp4j.TextEdit) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Document(org.eclipse.xtext.ide.server.Document) Test(org.junit.Test)

Example 8 with Document

use of org.eclipse.xtext.ide.server.Document in project xtext-core by eclipse.

the class AbstractLanguageServerTest method testRangeFormatting.

protected void testRangeFormatting(final Procedure1<? super DocumentRangeFormattingParams> paramsConfigurator, final Procedure1<? super RangeFormattingConfiguration> configurator) {
    try {
        @Extension final RangeFormattingConfiguration configuration = new RangeFormattingConfiguration();
        configuration.setFilePath(("MyModel." + this.fileExtension));
        configurator.apply(configuration);
        final FileInfo fileInfo = this.initializeContext(configuration);
        DocumentRangeFormattingParams _documentRangeFormattingParams = new DocumentRangeFormattingParams();
        final Procedure1<DocumentRangeFormattingParams> _function = (DocumentRangeFormattingParams it) -> {
            String _uri = fileInfo.getUri();
            TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(_uri);
            it.setTextDocument(_textDocumentIdentifier);
            it.setRange(configuration.getRange());
            if ((paramsConfigurator != null)) {
                paramsConfigurator.apply(it);
            }
        };
        DocumentRangeFormattingParams _doubleArrow = ObjectExtensions.<DocumentRangeFormattingParams>operator_doubleArrow(_documentRangeFormattingParams, _function);
        final CompletableFuture<List<? extends TextEdit>> changes = this.languageServer.rangeFormatting(_doubleArrow);
        String _contents = fileInfo.getContents();
        final Document result = new Document(1, _contents).applyChanges(ListExtensions.<TextEdit>reverse(CollectionLiterals.<TextEdit>newArrayList(((TextEdit[]) Conversions.unwrapArray(changes.get(), TextEdit.class)))));
        this.assertEqualsStricter(configuration.getExpectedText(), result.getContents());
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : Extension(org.eclipse.xtext.xbase.lib.Extension) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) RangeFormattingConfiguration(org.eclipse.xtext.testing.RangeFormattingConfiguration) FileInfo(org.eclipse.xtext.testing.FileInfo) DocumentRangeFormattingParams(org.eclipse.lsp4j.DocumentRangeFormattingParams) TextEdit(org.eclipse.lsp4j.TextEdit) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList) Document(org.eclipse.xtext.ide.server.Document)

Example 9 with Document

use of org.eclipse.xtext.ide.server.Document in project xtext-core by eclipse.

the class DefaultDocumentHighlightService method getDocumentHighlights.

@Override
public List<DocumentHighlight> getDocumentHighlights(final XtextResource resource, final int offset) {
    if (resource == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Resource was null.");
        }
        return emptyList();
    }
    final URI uri = resource.getURI();
    if (offset < 0) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Invalid offset argument. Offset must be a non-negative integer for resource: " + uri);
        }
        return emptyList();
    }
    final IParseResult parseResult = resource.getParseResult();
    if (parseResult == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Parse result was null for resource: " + uri);
        }
        return emptyList();
    }
    final ICompositeNode rootNode = parseResult.getRootNode();
    final String docContent = rootNode.getText();
    final int docLength = docContent.length();
    if (offset >= docLength) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.warn("Offset exceeds document lenght. Document was " + docLength + " and offset was: " + offset + " for resource: " + uri);
        }
        return emptyList();
    }
    final EObject selectedElemnt = offsetHelper.resolveElementAt(resource, offset);
    if (!isDocumentHighlightAvailableFor(selectedElemnt, resource, offset)) {
        return emptyList();
    }
    final Supplier<Document> docSupplier = Suppliers.memoize(() -> new Document(UNUSED_VERSION, docContent));
    Iterable<URI> targetURIs = getTargetURIs(selectedElemnt);
    if (!(targetURIs instanceof TargetURIs)) {
        final TargetURIs result = targetURIsProvider.get();
        result.addAllURIs(targetURIs);
        targetURIs = result;
    }
    final Builder<DocumentHighlight> resultBuilder = ImmutableList.<DocumentHighlight>builder();
    final Acceptor acceptor = (Acceptor2) (source, sourceURI, eReference, index, targetOrProxy, targetURI) -> {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(source, eReference, index);
        if (!isNullOrEmpty(region)) {
            resultBuilder.add(textRegionTransformer.apply(docSupplier.get(), region, DocumentHighlightKind.Read));
        }
    };
    referenceFinder.findReferences((TargetURIs) targetURIs, resource, acceptor, new NullProgressMonitor());
    if (resource.equals(selectedElemnt.eResource())) {
        final ITextRegion region = locationInFileProvider.getSignificantTextRegion(selectedElemnt);
        if (!isNullOrEmpty(region)) {
            resultBuilder.add(textRegionTransformer.apply(docSupplier.get(), region, DocumentHighlightKind.Write));
        }
    }
    return FluentIterable.from(resultBuilder.build()).toSortedList(comparator);
}
Also used : DocumentHighlight(org.eclipse.lsp4j.DocumentHighlight) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) Acceptor(org.eclipse.xtext.findReferences.IReferenceFinder.Acceptor) TargetURIs(org.eclipse.xtext.findReferences.TargetURIs) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) ITextRegion(org.eclipse.xtext.util.ITextRegion) EObject(org.eclipse.emf.ecore.EObject) ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 10 with Document

use of org.eclipse.xtext.ide.server.Document in project xtext-core by eclipse.

the class LanguageServerImpl method resolveCodeLens.

@Override
public CompletableFuture<CodeLens> resolveCodeLens(final CodeLens unresolved) {
    final URI uri = this.uninstallURI(unresolved);
    if ((uri == null)) {
        return CompletableFuture.<CodeLens>completedFuture(unresolved);
    }
    final Function1<CancelIndicator, CodeLens> _function = (CancelIndicator cancelIndicator) -> {
        final IResourceServiceProvider resourceServiceProvider = this.languagesRegistry.getResourceServiceProvider(uri);
        ICodeLensResolver _get = null;
        if (resourceServiceProvider != null) {
            _get = resourceServiceProvider.<ICodeLensResolver>get(ICodeLensResolver.class);
        }
        final ICodeLensResolver resolver = _get;
        if ((resolver == null)) {
            return unresolved;
        }
        final Function2<Document, XtextResource, CodeLens> _function_1 = (Document document, XtextResource resource) -> {
            final CodeLens result = resolver.resolveCodeLens(document, resource, unresolved, cancelIndicator);
            return result;
        };
        return this.workspaceManager.<CodeLens>doRead(uri, _function_1);
    };
    return this.requestManager.<CodeLens>runRead(_function);
}
Also used : ICodeLensResolver(org.eclipse.xtext.ide.server.codelens.ICodeLensResolver) CodeLens(org.eclipse.lsp4j.CodeLens) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) XtextResource(org.eclipse.xtext.resource.XtextResource) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI)

Aggregations

Document (org.eclipse.xtext.ide.server.Document)22 URI (org.eclipse.emf.common.util.URI)11 XtextResource (org.eclipse.xtext.resource.XtextResource)10 TextEdit (org.eclipse.lsp4j.TextEdit)8 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)8 Test (org.junit.Test)7 List (java.util.List)6 Resource (org.eclipse.emf.ecore.resource.Resource)4 CompletionList (org.eclipse.lsp4j.CompletionList)4 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)4 ArrayList (java.util.ArrayList)2 EObject (org.eclipse.emf.ecore.EObject)2 DocumentFormattingParams (org.eclipse.lsp4j.DocumentFormattingParams)2 Position (org.eclipse.lsp4j.Position)2 Range (org.eclipse.lsp4j.Range)2 TextDocumentIdentifier (org.eclipse.lsp4j.TextDocumentIdentifier)2 ProjectManager (org.eclipse.xtext.ide.server.ProjectManager)2 XtextResourceSet (org.eclipse.xtext.resource.XtextResourceSet)2 FileInfo (org.eclipse.xtext.testing.FileInfo)2 RangeFormattingConfiguration (org.eclipse.xtext.testing.RangeFormattingConfiguration)2