CodeLens for boot hints in VSCode

This commit is contained in:
BoykoAlex
2018-07-31 13:37:23 -04:00
parent 6772b05934
commit 374b5f0ad7
31 changed files with 571 additions and 200 deletions

View File

@@ -0,0 +1,52 @@
package org.springframework.tooling.ls.eclipse.commons;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.inlined.LineHeaderAnnotation;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
public class BootHeadlineAnnotation extends LineHeaderAnnotation {
public BootHeadlineAnnotation(Position position, ISourceViewer viewer) {
super(position, viewer);
}
// @Override
// public void draw(GC gc, StyledText textWidget, int offset, int length, Color color, int x, int y) {
// initGC(textWidget, color, gc);
//
// FontMetrics fontMetrics = gc.getFontMetrics();
// int height = fontMetrics.getHeight();
//
// Image bootImage = LanguageServerCommonsActivator.getInstance().getImageRegistry().get(LanguageServerCommonsActivator.BOOT_ICON_2X_KEY);
// Rectangle bootImgBounds = bootImage.getBounds();
// int width = (int) Math.round(bootImgBounds.width / (double) bootImgBounds.height * height);
//
// gc.drawImage(bootImage, bootImgBounds.x, bootImgBounds.y, bootImgBounds.width, bootImgBounds.height, x, y, width, height);
//
// int textX = x + width + 4;
// int textY = y;
//
// gc.drawText("CODE LENS goes here!", textX, textY);
// }
//
// /**
// * Initialize GC with given color and styled text background color and font.
// *
// * @param textWidget the text widget
// * @param color the color
// * @param gc the gc to initialize
// */
// private void initGC(StyledText textWidget, Color color, GC gc) {
// gc.setForeground(color);
// gc.setBackground(textWidget.getBackground());
// gc.setFont(textWidget.getFont());
// }
}

View File

@@ -12,7 +12,7 @@ package org.springframework.tooling.ls.eclipse.commons;
import java.util.List;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier;
public class HighlightParams {
@@ -23,15 +23,15 @@ public class HighlightParams {
// a update site.
private VersionedTextDocumentIdentifier doc;
private List<Range> ranges;
private List<CodeLens> codeLenses;
public HighlightParams() {
}
public HighlightParams(VersionedTextDocumentIdentifier doc, List<Range> ranges) {
public HighlightParams(VersionedTextDocumentIdentifier doc, List<CodeLens> codeLenses) {
super();
this.doc = doc;
this.ranges = ranges;
this.codeLenses = codeLenses;
}
public VersionedTextDocumentIdentifier getDoc() {
return doc;
@@ -39,15 +39,13 @@ public class HighlightParams {
public void setDoc(VersionedTextDocumentIdentifier doc) {
this.doc = doc;
}
public List<Range> getRanges() {
return ranges;
}
public void setRanges(List<Range> ranges) {
this.ranges = ranges;
public List<CodeLens> getCodeLenses() {
return codeLenses;
}
@Override
public String toString() {
return "HighlightParams [doc=" + doc + ", ranges=" + ranges + "]";
public void setCodeLenses(List<CodeLens> codeLenses) {
this.codeLenses = codeLenses;
}
}

View File

@@ -23,8 +23,11 @@ import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IStatusLineManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewerExtension2;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.source.Annotation;
import org.eclipse.jface.text.source.AnnotationPainter;
import org.eclipse.jface.text.source.IAnnotationAccess;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelExtension;
import org.eclipse.jface.text.source.ISourceViewer;
@@ -32,7 +35,7 @@ import org.eclipse.lsp4e.LSPEclipseUtils;
import org.eclipse.lsp4e.LanguageClientImpl;
import org.eclipse.lsp4e.LanguageServiceAccessor;
import org.eclipse.lsp4e.LanguageServiceAccessor.LSPDocumentInfo;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.VersionedTextDocumentIdentifier;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
@@ -137,6 +140,8 @@ public class STS4LanguageClientImpl extends LanguageClientImpl implements STS4La
*/
private Map<String, Annotation[]> currentAnnotations = new HashMap<>();
// private Map<ISourceViewer, InlinedAnnotationSupport> viewerInlinedAnnotationSupport = new WeakHashMap<>();
private synchronized void updateAnnotations(String target, ISourceViewer sourceViewer, IAnnotationModelExtension annotationModel) {
if (target!=null) {
HighlightParams highlightParams = currentHighlights.get(target);
@@ -146,10 +151,11 @@ public class STS4LanguageClientImpl extends LanguageClientImpl implements STS4La
if (toRemove==null) {
toRemove = new Annotation[0];
}
List<Range> highlights = highlightParams == null ? null : highlightParams.getRanges();
List<CodeLens> highlights = highlightParams == null ? null : highlightParams.getCodeLenses();
Map<Annotation, Position> newAnnotations = createAnnotations(doc, highlights);
annotationModel.replaceAnnotations(toRemove, newAnnotations);
currentAnnotations.put(target, newAnnotations.keySet().toArray(new Annotation[newAnnotations.size()]));
// updateInlinedAnnotations(sourceViewer, highlights);
}
}
}
@@ -166,12 +172,75 @@ public class STS4LanguageClientImpl extends LanguageClientImpl implements STS4La
return false;
}
private Map<Annotation, Position> createAnnotations(IDocument doc, List<Range> highlights) {
// private void updateInlinedAnnotations(final ISourceViewer sourceViewer, List<CodeLens> highlights) {
// InlinedAnnotationSupport support = viewerInlinedAnnotationSupport.get(sourceViewer);
// if (support == null) {
// final InlinedAnnotationSupport inlinedSupport = new InlinedAnnotationSupport();
// inlinedSupport.install(sourceViewer, createAnnotationPainter(sourceViewer));
// viewerInlinedAnnotationSupport.put(sourceViewer, inlinedSupport);
// sourceViewer.getTextWidget().addDisposeListener((e) -> {
// inlinedSupport.uninstall();
// viewerInlinedAnnotationSupport.remove(sourceViewer);
// });
// support = inlinedSupport;
// }
// Set<AbstractInlinedAnnotation> annotations = new HashSet<>();
// if (highlights==null) {
// highlights = ImmutableList.of();
// }
// IDocument doc = sourceViewer.getDocument();
// final InlinedAnnotationSupport inlinedSupport = support;
// highlights.stream().filter(hl -> hl.getCommand() != null && hl.getCommand().getTitle() != null).forEach(codeLens -> {
// try {
// Range rng = codeLens.getRange();
// int start = LSPEclipseUtils.toOffset(rng.getStart(), doc);
//
// // "Code Lens" line header annotation
// Position headerPos = new Position(start + 1, 1);
// BootHeadlineAnnotation headlineAnnotation = inlinedSupport.findExistingAnnotation(headerPos);
// if (headlineAnnotation == null) {
// headlineAnnotation = new BootHeadlineAnnotation(headerPos, sourceViewer);
// }
// headlineAnnotation.setText(codeLens.getCommand().getTitle());
// annotations.add(headlineAnnotation);
// } catch (BadLocationException e) {
// //ignore invalid highlights
// }
// });
// highlights.forEach(codeLens -> {
// });
// support.updateAnnotations(annotations);
// }
private static AnnotationPainter createAnnotationPainter(ISourceViewer viewer) {
IAnnotationAccess annotationAccess = new IAnnotationAccess() {
@Override
public Object getType(Annotation annotation) {
return annotation.getType();
}
@Override
public boolean isMultiLine(Annotation annotation) {
return true;
}
@Override
public boolean isTemporary(Annotation annotation) {
return true;
}
};
AnnotationPainter painter = new AnnotationPainter(viewer, annotationAccess);
((ITextViewerExtension2) viewer).addPainter(painter);
return painter;
}
private Map<Annotation, Position> createAnnotations(IDocument doc, List<CodeLens> highlights) {
ImmutableMap.Builder<Annotation, Position> annotations = ImmutableMap.builder();
if (highlights==null) {
highlights = ImmutableList.of();
}
for (Range rng : highlights) {
highlights.stream().map(CodeLens::getRange).forEach(rng -> {
try {
int start = LSPEclipseUtils.toOffset(rng.getStart(), doc);
int end = LSPEclipseUtils.toOffset(rng.getEnd(), doc);
@@ -180,7 +249,7 @@ public class STS4LanguageClientImpl extends LanguageClientImpl implements STS4La
} catch (BadLocationException e) {
//ignore invalid highlights
}
}
});
return annotations.build();
}