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;
}
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);
}
}
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);
}
}
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;
}
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);
}
}
Aggregations