下面列出了怎么用org.eclipse.lsp4j.SemanticHighlightingInformation的API类实例代码及写法,或者点击链接到github查看源代码。
protected List<SemanticHighlightingInformation> toInfos(IDocument document, List<HighlightedPositionCore> positions) {
Multimap<Integer, SemanticHighlightingTokens.Token> infos = HashMultimap.create();
for (HighlightedPositionCore position : positions) {
int[] lineAndColumn = JsonRpcHelpers.toLine(document, position.offset);
if (lineAndColumn == null) {
JavaLanguageServerPlugin.logError("Cannot locate line and column information for the semantic highlighting position: " + position + ". Skipping it.");
continue;
}
int line = lineAndColumn[0];
int character = lineAndColumn[1];
int length = position.length;
int scope = LOOKUP_TABLE.inverse().get(position.getHighlighting());
infos.put(line, new SemanticHighlightingTokens.Token(character, length, scope));
}
//@formatter:off
return infos.asMap().entrySet().stream()
.map(entry -> new SemanticHighlightingInformation(entry.getKey(), SemanticHighlightingTokens.encode(entry.getValue())))
.collect(Collectors.toList());
//@formatter:on
}
@Test
public void testDidOpen_autoBoxing() throws Exception {
String content = "package _package;\n"
+ "\n"
+ "public class A {\n" +
" public static void main(String[] args) {\n" +
" Integer integer = Integer.valueOf(36);\n" +
" System.out.println(10 + integer);\n" +
" }\n" +
"}";
int version = 1;
IJavaProject project = newEmptyProject();
IPackageFragmentRoot src = project.getPackageFragmentRoot(project.getProject().getFolder("src"));
IPackageFragment _package = src.createPackageFragment("_package", false, null);
ICompilationUnit unit = _package.createCompilationUnit("A.java", content, false, null);
openDocument(unit, unit.getSource(), version);
assertEquals(1, javaClient.params.size());
List<SemanticHighlightingInformation> lines = javaClient.params.get(0).getLines();
assertEquals(4, lines.size());
SemanticHighlightingInformation line5 = FluentIterable.from(lines).firstMatch(line -> line.getLine() == 5).get();
SemanticHighlightingTokens.Token unboxingToken = FluentIterable.from(decode(line5.getTokens())).firstMatch(token -> token.character == 28).get();
assertEquals(unboxingToken.length, 7);
assertThat(SemanticHighlightingService.getScopes(unboxingToken.scope), hasItem("variable.other.autoboxing.java"));
}
protected List<SemanticHighlightingInformation> appendEmptyLineTokens(final List<SemanticHighlightingInformation> infos, final Document document) {
final int lineCount = document.getLineCount();
final Function<SemanticHighlightingInformation, Integer> _function = (SemanticHighlightingInformation it) -> {
return Integer.valueOf(it.getLine());
};
final HashMap<Integer, SemanticHighlightingInformation> tokens = Maps.<Integer, SemanticHighlightingInformation>newHashMap(Maps.<Integer, SemanticHighlightingInformation>uniqueIndex(infos, _function));
ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, lineCount, true);
for (final Integer i : _doubleDotLessThan) {
boolean _containsKey = tokens.containsKey(i);
boolean _not = (!_containsKey);
if (_not) {
SemanticHighlightingInformation _semanticHighlightingInformation = new SemanticHighlightingInformation((i).intValue(), null);
tokens.put(i, _semanticHighlightingInformation);
}
}
return IterableExtensions.<SemanticHighlightingInformation>toList(tokens.values());
}
@Override
public int compare(SemanticHighlightingInformation left, SemanticHighlightingInformation right) {
//@formatter:off
return ComparisonChain.start()
.compare(left.getLine(), right.getLine())
.result();
//@formatter:on
}
public List<Position> install(ICompilationUnit unit) throws JavaModelException, BadPositionCategoryException {
if (enabled.get()) {
List<HighlightedPositionCore> positions = calculateHighlightedPositions(unit, false);
String uri = JDTUtils.getFileURI(unit.getResource());
this.cache.put(uri, positions);
if (!positions.isEmpty()) {
IDocument document = JsonRpcHelpers.toDocument(unit.getBuffer());
List<SemanticHighlightingInformation> infos = toInfos(document, positions);
VersionedTextDocumentIdentifier textDocument = new VersionedTextDocumentIdentifier(uri, 1);
notifyClient(textDocument, infos);
}
return ImmutableList.copyOf(positions);
}
return emptyList();
}
public void update(VersionedTextDocumentIdentifier textDocument, List<HighlightedPositionDiffContext> diffContexts) throws BadLocationException, BadPositionCategoryException, JavaModelException {
if (enabled.get() && !diffContexts.isEmpty()) {
List<SemanticHighlightingInformation> deltaInfos = newArrayList();
for (HighlightedPositionDiffContext context : diffContexts) {
deltaInfos.addAll(diffCalculator.getDiffInfos(context));
}
if (!deltaInfos.isEmpty()) {
notifyClient(textDocument, deltaInfos);
}
}
}
protected void notifyClient(VersionedTextDocumentIdentifier textDocument, List<SemanticHighlightingInformation> infos) {
if (infos.isEmpty()) {
return;
}
this.connection.semanticHighlighting(new SemanticHighlightingParams(textDocument, infos));
}
@Override
protected String toExpectation(final Object it) {
if (it instanceof Integer) {
return _toExpectation((Integer)it);
} else if (it instanceof List) {
return _toExpectation((List<?>)it);
} else if (it instanceof DocumentHighlightKind) {
return _toExpectation((DocumentHighlightKind)it);
} else if (it instanceof String) {
return _toExpectation((String)it);
} else if (it instanceof VersionedTextDocumentIdentifier) {
return _toExpectation((VersionedTextDocumentIdentifier)it);
} else if (it instanceof Pair) {
return _toExpectation((Pair<SemanticHighlightingInformation, List<List<String>>>)it);
} else if (it == null) {
return _toExpectation((Void)null);
} else if (it instanceof Map) {
return _toExpectation((Map<Object, Object>)it);
} else if (it instanceof CodeAction) {
return _toExpectation((CodeAction)it);
} else if (it instanceof CodeLens) {
return _toExpectation((CodeLens)it);
} else if (it instanceof Command) {
return _toExpectation((Command)it);
} else if (it instanceof CompletionItem) {
return _toExpectation((CompletionItem)it);
} else if (it instanceof DocumentHighlight) {
return _toExpectation((DocumentHighlight)it);
} else if (it instanceof DocumentSymbol) {
return _toExpectation((DocumentSymbol)it);
} else if (it instanceof Hover) {
return _toExpectation((Hover)it);
} else if (it instanceof Location) {
return _toExpectation((Location)it);
} else if (it instanceof MarkupContent) {
return _toExpectation((MarkupContent)it);
} else if (it instanceof Position) {
return _toExpectation((Position)it);
} else if (it instanceof Range) {
return _toExpectation((Range)it);
} else if (it instanceof ResourceOperation) {
return _toExpectation((ResourceOperation)it);
} else if (it instanceof SignatureHelp) {
return _toExpectation((SignatureHelp)it);
} else if (it instanceof SymbolInformation) {
return _toExpectation((SymbolInformation)it);
} else if (it instanceof TextDocumentEdit) {
return _toExpectation((TextDocumentEdit)it);
} else if (it instanceof TextEdit) {
return _toExpectation((TextEdit)it);
} else if (it instanceof WorkspaceEdit) {
return _toExpectation((WorkspaceEdit)it);
} else if (it instanceof Either) {
return _toExpectation((Either<?, ?>)it);
} else {
throw new IllegalArgumentException("Unhandled parameter types: " +
Arrays.<Object>asList(it).toString());
}
}
protected String toExpectation(final Object it) {
if (it instanceof Integer) {
return _toExpectation((Integer)it);
} else if (it instanceof List) {
return _toExpectation((List<?>)it);
} else if (it instanceof DocumentHighlightKind) {
return _toExpectation((DocumentHighlightKind)it);
} else if (it instanceof String) {
return _toExpectation((String)it);
} else if (it instanceof VersionedTextDocumentIdentifier) {
return _toExpectation((VersionedTextDocumentIdentifier)it);
} else if (it instanceof Pair) {
return _toExpectation((Pair<SemanticHighlightingInformation, List<List<String>>>)it);
} else if (it == null) {
return _toExpectation((Void)null);
} else if (it instanceof Map) {
return _toExpectation((Map<Object, Object>)it);
} else if (it instanceof CodeAction) {
return _toExpectation((CodeAction)it);
} else if (it instanceof CodeLens) {
return _toExpectation((CodeLens)it);
} else if (it instanceof Command) {
return _toExpectation((Command)it);
} else if (it instanceof CompletionItem) {
return _toExpectation((CompletionItem)it);
} else if (it instanceof DocumentHighlight) {
return _toExpectation((DocumentHighlight)it);
} else if (it instanceof DocumentSymbol) {
return _toExpectation((DocumentSymbol)it);
} else if (it instanceof Hover) {
return _toExpectation((Hover)it);
} else if (it instanceof Location) {
return _toExpectation((Location)it);
} else if (it instanceof MarkupContent) {
return _toExpectation((MarkupContent)it);
} else if (it instanceof Position) {
return _toExpectation((Position)it);
} else if (it instanceof Range) {
return _toExpectation((Range)it);
} else if (it instanceof ResourceOperation) {
return _toExpectation((ResourceOperation)it);
} else if (it instanceof SignatureHelp) {
return _toExpectation((SignatureHelp)it);
} else if (it instanceof SymbolInformation) {
return _toExpectation((SymbolInformation)it);
} else if (it instanceof TextDocumentEdit) {
return _toExpectation((TextDocumentEdit)it);
} else if (it instanceof TextEdit) {
return _toExpectation((TextEdit)it);
} else if (it instanceof WorkspaceEdit) {
return _toExpectation((WorkspaceEdit)it);
} else if (it instanceof Either) {
return _toExpectation((Either<?, ?>)it);
} else {
throw new IllegalArgumentException("Unhandled parameter types: " +
Arrays.<Object>asList(it).toString());
}
}
public void update(final ILanguageServerAccess.Context context) {
this.checkInitialized();
Resource _resource = context.getResource();
boolean _not = (!(_resource instanceof XtextResource));
if (_not) {
return;
}
boolean _isDocumentOpen = context.isDocumentOpen();
boolean _not_1 = (!_isDocumentOpen);
if (_not_1) {
return;
}
Resource _resource_1 = context.getResource();
final XtextResource resource = ((XtextResource) _resource_1);
IResourceServiceProvider _resourceServiceProvider = resource.getResourceServiceProvider();
ISemanticHighlightingCalculator _get = null;
if (_resourceServiceProvider!=null) {
_get=_resourceServiceProvider.<ISemanticHighlightingCalculator>get(ISemanticHighlightingCalculator.class);
}
final ISemanticHighlightingCalculator calculator = _get;
IResourceServiceProvider _resourceServiceProvider_1 = resource.getResourceServiceProvider();
ISemanticHighlightingStyleToTokenMapper _get_1 = null;
if (_resourceServiceProvider_1!=null) {
_get_1=_resourceServiceProvider_1.<ISemanticHighlightingStyleToTokenMapper>get(ISemanticHighlightingStyleToTokenMapper.class);
}
final ISemanticHighlightingStyleToTokenMapper mapper = _get_1;
if (((calculator == null) || this.isIgnoredMapper(mapper))) {
return;
}
final Document document = context.getDocument();
final MergingHighlightedPositionAcceptor acceptor = new MergingHighlightedPositionAcceptor(calculator);
calculator.provideHighlightingFor(resource, acceptor, CancelIndicator.NullImpl);
final Function1<LightweightPosition, List<SemanticHighlightingRegistry.HighlightedRange>> _function = (LightweightPosition position) -> {
final Function1<String, SemanticHighlightingRegistry.HighlightedRange> _function_1 = (String id) -> {
final Position start = document.getPosition(position.getOffset());
int _offset = position.getOffset();
int _length = position.getLength();
int _plus = (_offset + _length);
final Position end = document.getPosition(_plus);
final int scope = this.getIndex(mapper.toScopes(id));
return new SemanticHighlightingRegistry.HighlightedRange(start, end, scope);
};
return ListExtensions.<String, SemanticHighlightingRegistry.HighlightedRange>map(((List<String>)Conversions.doWrapArray(position.getIds())), _function_1);
};
final Iterable<SemanticHighlightingRegistry.HighlightedRange> ranges = Iterables.<SemanticHighlightingRegistry.HighlightedRange>concat(ListExtensions.<LightweightPosition, List<SemanticHighlightingRegistry.HighlightedRange>>map(acceptor.getPositions(), _function));
final List<SemanticHighlightingInformation> lines = this.toSemanticHighlightingInformation(ranges, document);
final VersionedTextDocumentIdentifier textDocument = this.toVersionedTextDocumentIdentifier(context);
SemanticHighlightingParams _semanticHighlightingParams = new SemanticHighlightingParams(textDocument, lines);
this.notifyClient(_semanticHighlightingParams);
}
public SemanticHighlightingParams(@NonNull final VersionedTextDocumentIdentifier textDocument, @NonNull final List<SemanticHighlightingInformation> lines) {
this.textDocument = Preconditions.<VersionedTextDocumentIdentifier>checkNotNull(textDocument, "textDocument");
this.lines = Preconditions.<List<SemanticHighlightingInformation>>checkNotNull(lines, "lines");
}
/**
* An array of semantic highlighting information.
*/
@Pure
@NonNull
public List<SemanticHighlightingInformation> getLines() {
return this.lines;
}
/**
* An array of semantic highlighting information.
*/
public void setLines(@NonNull final List<SemanticHighlightingInformation> lines) {
this.lines = Preconditions.checkNotNull(lines, "lines");
}