Search in sources :

Example 1 with XDocument

use of org.eclipse.n4js.xtext.ide.server.XDocument in project n4js by eclipse.

the class N4JSRenameService method rename.

// #################################################################################################################
// rename
// 
// In case of #rename() we entirely replace the implementation from the super class by our own implementation (in
// order to reuse reference finding as far as possible, for consistency):
@Override
public WorkspaceEdit rename(Options options) {
    RenameParams renameParams = options.getRenameParams();
    CancelIndicator cancelIndicator = options.getCancelIndicator();
    ResourceTaskContext rtc = resourceTaskManager.currentContext();
    if (rtc == null || !rtc.isTemporary()) {
        throw new IllegalStateException("N4JSRenameService#rename expects to be invoked within a temporary resource task context");
    }
    ResourceSet resourceSet = rtc.getResourceSet();
    XtextResource resource = rtc.getResource();
    XDocument document = rtc.getDocument();
    int offset = document.getOffSet(renameParams.getPosition());
    // in case the temporary resource task context we are using was created without 'resolveAndValidate' we need to
    // trigger resolution (not validation) here:
    EcoreUtil2.resolveLazyCrossReferences(resource, cancelIndicator);
    EObject element = getElementToBeRenamed(resource, offset);
    if (element == null) {
        // empty edit
        return new WorkspaceEdit();
    }
    WorkspaceEdit result = computeRenameEdits(resourceSet, element, renameParams.getNewName(), resourceTaskManager.createLiveScopeIndex(), cancelIndicator);
    return result;
}
Also used : RenameParams(org.eclipse.lsp4j.RenameParams) EObject(org.eclipse.emf.ecore.EObject) ResourceTaskContext(org.eclipse.n4js.xtext.ide.server.ResourceTaskContext) XtextResource(org.eclipse.xtext.resource.XtextResource) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) XDocument(org.eclipse.n4js.xtext.ide.server.XDocument)

Example 2 with XDocument

use of org.eclipse.n4js.xtext.ide.server.XDocument in project n4js by eclipse.

the class AbstractIdeTest method applyTextEdits.

/**
 * Applies the given text edits to the given character sequence and returns the resulting string.
 */
protected static String applyTextEdits(CharSequence oldContent, Iterable<? extends TextEdit> textEdits) {
    XDocument oldDocument = new XDocument(0, oldContent.toString(), true, true);
    XDocument newDocument = oldDocument.applyChanges(textEdits);
    return newDocument.getContents();
}
Also used : XDocument(org.eclipse.n4js.xtext.ide.server.XDocument)

Example 3 with XDocument

use of org.eclipse.n4js.xtext.ide.server.XDocument in project n4js by eclipse.

the class N4JSCodeActionService method doApplyToFile.

/**
 * Applies given quick fix to file with given URI and waits for and returns the resulting edits.
 */
protected Map<String, List<TextEdit>> doApplyToFile(URI uri, String issueCode, QuickFixImplementation quickfix, CancelIndicator cancelIndicator) {
    TextEditCollector collector = new TextEditCollector();
    TextDocumentIdentifier textDocId = new TextDocumentIdentifier(uriExtensions.toUriString(uri));
    ResourceTaskManager resourceTaskManager = languageServer.getResourceTaskManager();
    resourceTaskManager.<Void>runInTemporaryContext(uri, "doApplyToFile", false, cancelIndicator, (ofc, ci) -> {
        XtextResource res = ofc.getResource();
        List<? extends Issue> issues = ofc.resolveAndValidateResource(ci);
        XDocument doc = ofc.getDocument();
        for (Issue issue : issues) {
            if (issueCode.equals(issue.getCode())) {
                Options newOptions = createOptions(res, doc, textDocId, issue, ci);
                quickfix.compute(issueCode, newOptions, collector);
            }
        }
        return null;
    }).join();
    return collector.allEdits;
}
Also used : URI(org.eclipse.emf.common.util.URI) XLanguageServerImpl(org.eclipse.n4js.xtext.ide.server.XLanguageServerImpl) Inject(com.google.inject.Inject) N4JSProjectConfigSnapshot(org.eclipse.n4js.workspace.N4JSProjectConfigSnapshot) HashMap(java.util.HashMap) Diagnostic(org.eclipse.lsp4j.Diagnostic) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) Multimap(com.google.common.collect.Multimap) CodeActionContext(org.eclipse.lsp4j.CodeActionContext) XDocument(org.eclipse.n4js.xtext.ide.server.XDocument) UriExtensions(org.eclipse.xtext.ide.server.UriExtensions) ArrayList(java.util.ArrayList) CodeActionParams(org.eclipse.lsp4j.CodeActionParams) Strings(com.google.common.base.Strings) TextEdit(org.eclipse.lsp4j.TextEdit) HashMultimap(com.google.common.collect.HashMultimap) Lists(com.google.common.collect.Lists) TextDocumentFrontend(org.eclipse.n4js.xtext.ide.server.TextDocumentFrontend) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) Map(java.util.Map) Either(org.eclipse.lsp4j.jsonrpc.messages.Either) N4JSCommandService(org.eclipse.n4js.ide.server.commands.N4JSCommandService) Method(java.lang.reflect.Method) CodeAction(org.eclipse.lsp4j.CodeAction) N4JSWorkspaceConfigSnapshot(org.eclipse.n4js.workspace.N4JSWorkspaceConfigSnapshot) XtextResource(org.eclipse.xtext.resource.XtextResource) IssueToDiagnosticConverter(org.eclipse.n4js.xtext.ide.server.issues.IssueToDiagnosticConverter) OperationCanceledManager(org.eclipse.xtext.service.OperationCanceledManager) ICodeActionService2(org.eclipse.xtext.ide.server.codeActions.ICodeActionService2) Injector(com.google.inject.Injector) ConcurrentIndex(org.eclipse.n4js.xtext.ide.server.build.ConcurrentIndex) List(java.util.List) N4JSResource(org.eclipse.n4js.resource.N4JSResource) IterableExtensions(org.eclipse.xtext.xbase.lib.IterableExtensions) Command(org.eclipse.lsp4j.Command) WorkspaceEdit(org.eclipse.lsp4j.WorkspaceEdit) Issue(org.eclipse.xtext.validation.Issue) N4JSSourceFolderSnapshot(org.eclipse.n4js.workspace.N4JSSourceFolderSnapshot) ResourceTaskManager(org.eclipse.n4js.xtext.ide.server.ResourceTaskManager) Modifier(java.lang.reflect.Modifier) Collections(java.util.Collections) Singleton(com.google.inject.Singleton) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) ResourceTaskManager(org.eclipse.n4js.xtext.ide.server.ResourceTaskManager) Issue(org.eclipse.xtext.validation.Issue) XtextResource(org.eclipse.xtext.resource.XtextResource) ArrayList(java.util.ArrayList) List(java.util.List) XDocument(org.eclipse.n4js.xtext.ide.server.XDocument)

Example 4 with XDocument

use of org.eclipse.n4js.xtext.ide.server.XDocument in project n4js by eclipse.

the class AbstractCompletionTest method applyCompletionItem.

private String applyCompletionItem(String oldContent, Position pos, CompletionItem item) {
    String content = oldContent;
    TextEdit mainEdit = item.getTextEdit().getLeft();
    if (mainEdit != null) {
        content = applyTextEdits(content, Collections.singleton(mainEdit));
    } else {
        String toInsert = item.getInsertText() != null ? item.getInsertText() : item.getLabel();
        if (toInsert != null) {
            XDocument doc = new XDocument(0, content.toString(), true, true);
            doc.applyChanges(Collections.singleton(new TextEdit(new Range(pos, pos), toInsert)));
            content = doc.getContents();
        }
    }
    List<TextEdit> additionalEdits = item.getAdditionalTextEdits();
    if (additionalEdits != null && !additionalEdits.isEmpty()) {
        content = applyTextEdits(content, additionalEdits);
    }
    return content;
}
Also used : TextEdit(org.eclipse.lsp4j.TextEdit) Range(org.eclipse.lsp4j.Range) XDocument(org.eclipse.n4js.xtext.ide.server.XDocument)

Aggregations

XDocument (org.eclipse.n4js.xtext.ide.server.XDocument)4 TextEdit (org.eclipse.lsp4j.TextEdit)2 WorkspaceEdit (org.eclipse.lsp4j.WorkspaceEdit)2 XtextResource (org.eclipse.xtext.resource.XtextResource)2 Strings (com.google.common.base.Strings)1 HashMultimap (com.google.common.collect.HashMultimap)1 Lists (com.google.common.collect.Lists)1 Multimap (com.google.common.collect.Multimap)1 Inject (com.google.inject.Inject)1 Injector (com.google.inject.Injector)1 Singleton (com.google.inject.Singleton)1 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 URI (org.eclipse.emf.common.util.URI)1 EObject (org.eclipse.emf.ecore.EObject)1