Skip to content

Commit

Permalink
Initialize support for documentLink (see #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
angelozerr committed Aug 11, 2018
1 parent 84501fb commit 3218682
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.logging.Logger;

import org.eclipse.lsp4j.CompletionOptions;
import org.eclipse.lsp4j.DocumentLinkOptions;
import org.eclipse.lsp4j.InitializeParams;
import org.eclipse.lsp4j.InitializeResult;
import org.eclipse.lsp4j.TextDocumentSyncKind;
Expand Down Expand Up @@ -64,7 +65,8 @@ public XMLLanguageServer() {
@Override
public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
xmlTextDocumentService.updateClientCapabilities(params.getCapabilities());
// FIXME: use ServerCapabilities when https://github.com/eclipse/lsp4j/issues/169 will be ready
// FIXME: use ServerCapabilities when
// https://github.com/eclipse/lsp4j/issues/169 will be ready
ExtendedServerCapabilities capabilities = new ExtendedServerCapabilities();
capabilities.setTextDocumentSync(TextDocumentSyncKind.Full);
capabilities.setDocumentSymbolProvider(true);
Expand All @@ -75,6 +77,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
capabilities.setHoverProvider(true);
capabilities.setRenameProvider(true);
capabilities.setFoldingRangeProvider(true);
capabilities.setDocumentLinkProvider(new DocumentLinkOptions(true));
InitializeResult result = new InitializeResult(capabilities);
LogHelper.initializeRootLogger(languageClient, getInitializationOptions(params));
return CompletableFuture.completedFuture(result);
Expand Down Expand Up @@ -121,8 +124,9 @@ public LanguageClient getLanguageClient() {
public XMLLanguageService getXMLLanguageService() {
return xmlLanguageService;
}

// FIXME: remove this method when https://github.com/eclipse/lsp4j/issues/169 will be ready

// FIXME: remove this method when https://github.com/eclipse/lsp4j/issues/169
// will be ready
@Override
public CompletableFuture<List<? extends FoldingRange>> foldingRanges(FoldingRangeRequestParams params) {
return xmlTextDocumentService.foldingRanges(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.eclipse.lsp4j.DidSaveTextDocumentParams;
import org.eclipse.lsp4j.DocumentFormattingParams;
import org.eclipse.lsp4j.DocumentHighlight;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.DocumentLinkParams;
import org.eclipse.lsp4j.DocumentOnTypeFormattingParams;
import org.eclipse.lsp4j.DocumentRangeFormattingParams;
import org.eclipse.lsp4j.DocumentSymbolParams;
Expand Down Expand Up @@ -229,7 +231,8 @@ public void didClose(DidCloseTextDocumentParams params) {

}

// FIXME: un-comment when https://github.com/eclipse/lsp4j/issues/169 will be ready
// FIXME: un-comment when https://github.com/eclipse/lsp4j/issues/169 will be
// ready
// @Override
public CompletableFuture<List<? extends FoldingRange>> foldingRanges(FoldingRangeRequestParams params) {
return computeAsync((monitor) -> {
Expand All @@ -238,6 +241,14 @@ public CompletableFuture<List<? extends FoldingRange>> foldingRanges(FoldingRang
});
}

@Override
public CompletableFuture<List<DocumentLink>> documentLink(DocumentLinkParams params) {
return computeAsync((monitor) -> {
TextDocument document = documents.get(params.getTextDocument().getUri());
return getXMLLanguageService().findDocumentLinks(document);
});
}

@Override
public void didSave(DidSaveTextDocumentParams params) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Copyright (c) 2018 Angelo ZERR
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Angelo Zerr <[email protected]> - initial API and implementation
*/
package org.eclipse.lsp4xml.services;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4xml.commons.TextDocument;
import org.eclipse.lsp4xml.services.extensions.XMLExtensionsRegistry;

/**
* XML document link support.
*
*/
class XMLDocumentLink {

private final XMLExtensionsRegistry extensionsRegistry;

public XMLDocumentLink(XMLExtensionsRegistry extensionsRegistry) {
this.extensionsRegistry = extensionsRegistry;
}

public List<DocumentLink> findDocumentLinks(TextDocument document) {
List<DocumentLink> newLinks = new ArrayList<>();
// TODO: call extension
/*let rootAbsoluteUrl: Uri | null = null;
Scanner scanner = XMLScanner.createScanner(document.getText(), 0);
TokenType token = scanner.scan();
let afterHrefOrSrc = false;
let afterBase = false;
let base: string | undefined = void 0;
while (token != TokenType.EOS) {
switch (token) {
case TokenType.StartTag:
if (!base) {
let tagName = scanner.getTokenText().toLowerCase();
afterBase = tagName === 'base';
}
break;
case TokenType.AttributeName:
let attributeName = scanner.getTokenText().toLowerCase();
afterHrefOrSrc = attributeName === 'src' || attributeName === 'href';
break;
case TokenType.AttributeValue:
if (afterHrefOrSrc) {
let attributeValue = scanner.getTokenText();
if (!afterBase) { // don't highlight the base link itself
let link = createLink(document, documentContext, attributeValue, scanner.getTokenOffset(), scanner.getTokenEnd(), base);
if (link) {
newLinks.push(link);
}
}
if (afterBase && typeof base === 'undefined') {
base = normalizeRef(attributeValue, document.languageId);
if (base && documentContext) {
base = documentContext.resolveReference(base, document.uri);
}
}
afterBase = false;
afterHrefOrSrc = false;
}
break;
}
token = scanner.scan();
}*/
return newLinks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DocumentHighlight;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.FormattingOptions;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.Position;
Expand Down Expand Up @@ -47,6 +48,7 @@ public class XMLLanguageService extends XMLExtensionsRegistry {
private final XMLHover hover;
private final XMLDiagnostics diagnostics;
private final XMLFoldings foldings;
private XMLDocumentLink documentLink;

public XMLLanguageService() {
this.formatter = new XMLFormatter(this);
Expand All @@ -56,6 +58,7 @@ public XMLLanguageService() {
this.hover = new XMLHover(this);
this.diagnostics = new XMLDiagnostics(this);
this.foldings = new XMLFoldings(this);
this.documentLink = new XMLDocumentLink(this);
}

public List<? extends TextEdit> format(TextDocument document, Range range, FormattingOptions options) {
Expand Down Expand Up @@ -95,4 +98,8 @@ public WorkspaceEdit doRename(XMLDocument xmlDocument, Position position, String
return new WorkspaceEdit(changes);
}

public List<DocumentLink> findDocumentLinks(TextDocument document) {
return documentLink.findDocumentLinks(document);
}

}

1 comment on commit 3218682

@fbricon
Copy link
Contributor

@fbricon fbricon commented on 3218682 Nov 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No tests?

Please sign in to comment.