Search in sources :

Example 11 with Document

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

the class WorkspaceManager method getDocument.

protected Document getDocument(final XtextResource resource) {
    Document _elvis = null;
    Document _get = this.openDocuments.get(resource.getURI());
    if (_get != null) {
        _elvis = _get;
    } else {
        String _text = resource.getParseResult().getRootNode().getText();
        Document _document = new Document(1, _text);
        _elvis = _document;
    }
    return _elvis;
}
Also used : Document(org.eclipse.xtext.ide.server.Document)

Example 12 with Document

use of org.eclipse.xtext.ide.server.Document 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 13 with Document

use of org.eclipse.xtext.ide.server.Document 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)

Example 14 with Document

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

the class RenameService method rename.

@Override
public WorkspaceEdit rename(final WorkspaceManager workspaceManager, final RenameParams renameParams, final CancelIndicator cancelIndicator) {
    WorkspaceEdit _xblockexpression = null;
    {
        final URI uri = this._uriExtensions.toUri(renameParams.getTextDocument().getUri());
        final ServerRefactoringIssueAcceptor issueAcceptor = this.issueProvider.get();
        final Function2<Document, XtextResource, WorkspaceEdit> _function = (Document document, XtextResource resource) -> {
            final ProjectManager projectManager = workspaceManager.getProjectManager(uri);
            final XtextResourceSet resourceSet = projectManager.createNewResourceSet(projectManager.getIndexState().getResourceDescriptions());
            resourceSet.getLoadOptions().put(ResourceDescriptionsProvider.LIVE_SCOPE, Boolean.valueOf(true));
            final int offset = document.getOffSet(renameParams.getPosition());
            final WorkspaceEdit workspaceEdit = new WorkspaceEdit();
            final Resource xtextResource = resourceSet.getResource(resource.getURI(), true);
            if ((xtextResource instanceof XtextResource)) {
                final EObject element = this._eObjectAtOffsetHelper.resolveElementAt(((XtextResource) xtextResource), offset);
                if (((element == null) || element.eIsProxy())) {
                    StringConcatenation _builder = new StringConcatenation();
                    _builder.append("No element found at position line:");
                    int _line = renameParams.getPosition().getLine();
                    _builder.append(_line);
                    _builder.append(" column:");
                    int _character = renameParams.getPosition().getCharacter();
                    _builder.append(_character);
                    issueAcceptor.add(RefactoringIssueAcceptor.Severity.FATAL, _builder.toString());
                } else {
                    String _newName = renameParams.getNewName();
                    URI _uRI = EcoreUtil.getURI(element);
                    final RenameChange change = new RenameChange(_newName, _uRI);
                    final IChangeSerializer changeSerializer = this.changeSerializerProvider.get();
                    final RenameContext context = new RenameContext(Collections.<RenameChange>unmodifiableList(CollectionLiterals.<RenameChange>newArrayList(change)), resourceSet, changeSerializer, issueAcceptor);
                    this.renameStrategy.applyRename(context);
                    final ChangeConverter changeConverter = this.converterFactory.create(workspaceManager, workspaceEdit);
                    changeSerializer.applyModifications(changeConverter);
                }
            } else {
                issueAcceptor.add(RefactoringIssueAcceptor.Severity.FATAL, "Loaded resource is not an XtextResource", resource.getURI());
            }
            return workspaceEdit;
        };
        _xblockexpression = workspaceManager.<WorkspaceEdit>doRead(uri, _function);
    }
    return _xblockexpression;
}
Also used : RenameChange(org.eclipse.xtext.ide.refactoring.RenameChange) RenameContext(org.eclipse.xtext.ide.refactoring.RenameContext) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) ChangeConverter(org.eclipse.xtext.ide.server.rename.ChangeConverter) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) XtextResource(org.eclipse.xtext.resource.XtextResource) Function2(org.eclipse.xtext.xbase.lib.Functions.Function2) Document(org.eclipse.xtext.ide.server.Document) URI(org.eclipse.emf.common.util.URI) ServerRefactoringIssueAcceptor(org.eclipse.xtext.ide.server.rename.ServerRefactoringIssueAcceptor) ProjectManager(org.eclipse.xtext.ide.server.ProjectManager) IChangeSerializer(org.eclipse.xtext.ide.serializer.IChangeSerializer) XtextResourceSet(org.eclipse.xtext.resource.XtextResourceSet) EObject(org.eclipse.emf.ecore.EObject) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation)

Example 15 with Document

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

the class AbstractLanguageServerTest method testFormatting.

protected void testFormatting(final Procedure1<? super DocumentFormattingParams> paramsConfigurator, final Procedure1<? super FormattingConfiguration> configurator) {
    try {
        @Extension final FormattingConfiguration configuration = new FormattingConfiguration();
        configuration.setFilePath(("MyModel." + this.fileExtension));
        configurator.apply(configuration);
        final FileInfo fileInfo = this.initializeContext(configuration);
        DocumentFormattingParams _documentFormattingParams = new DocumentFormattingParams();
        final Procedure1<DocumentFormattingParams> _function = (DocumentFormattingParams it) -> {
            String _uri = fileInfo.getUri();
            TextDocumentIdentifier _textDocumentIdentifier = new TextDocumentIdentifier(_uri);
            it.setTextDocument(_textDocumentIdentifier);
            if ((paramsConfigurator != null)) {
                paramsConfigurator.apply(it);
            }
        };
        DocumentFormattingParams _doubleArrow = ObjectExtensions.<DocumentFormattingParams>operator_doubleArrow(_documentFormattingParams, _function);
        final CompletableFuture<List<? extends TextEdit>> changes = this.languageServer.formatting(_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) FileInfo(org.eclipse.xtext.testing.FileInfo) DocumentFormattingParams(org.eclipse.lsp4j.DocumentFormattingParams) TextEdit(org.eclipse.lsp4j.TextEdit) List(java.util.List) CompletionList(org.eclipse.lsp4j.CompletionList) FormattingConfiguration(org.eclipse.xtext.testing.FormattingConfiguration) RangeFormattingConfiguration(org.eclipse.xtext.testing.RangeFormattingConfiguration) Document(org.eclipse.xtext.ide.server.Document)

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