Skip to content

Commit

Permalink
feat: update lsp4mp to 0.7.1
Browse files Browse the repository at this point in the history
Signed-off-by: Fred Bricon <[email protected]>
  • Loading branch information
fbricon committed May 31, 2023
1 parent 0f37ca5 commit 0b6e94b
Show file tree
Hide file tree
Showing 27 changed files with 748 additions and 56 deletions.
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

plugins {
id "org.jetbrains.intellij" version "1.10.1"
id "org.jetbrains.intellij" version "1.13.3"
id 'idea'
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.6.0'
Expand Down Expand Up @@ -149,20 +149,20 @@ dependencies {
implementation("io.quarkus:quarkus-arc:$quarkusVersion") {
transitive = false
}
implementation 'org.eclipse.lsp4mp:org.eclipse.lsp4mp.ls:0.6.0'
implementation "org.eclipse.lsp4mp:org.eclipse.lsp4mp.ls:$lsp4mpVersion"
implementation 'org.eclipse.lsp4j:org.eclipse.lsp4j:0.15.0'
//required by lsp4j as the version from IJ is incompatible
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'com.vladsch.flexmark:flexmark:0.50.50'
testImplementation 'com.redhat.devtools.intellij:intellij-common-ui-test-library:0.2.0'
lsp('org.eclipse.lsp4mp:org.eclipse.lsp4mp.ls:0.6.0:uber') {
lsp("org.eclipse.lsp4mp:org.eclipse.lsp4mp.ls:$lsp4mpVersion:uber") {
transitive = false
}
lsp ('com.redhat.microprofile:com.redhat.quarkus.ls:0.13.0') {
lsp ("com.redhat.microprofile:com.redhat.quarkus.ls:$quarkusLsVersion") {
transitive = false
}
implementation 'com.redhat.microprofile:com.redhat.qute.ls:0.13.0'
lsp ('com.redhat.microprofile:com.redhat.qute.ls:0.13.0:uber') {
implementation "com.redhat.microprofile:com.redhat.qute.ls:$quarkusLsVersion"
lsp ("com.redhat.microprofile:com.redhat.qute.ls:$quarkusLsVersion:uber") {
transitive = false
}
implementation files(new File(buildDir, 'server')) {
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ projectVersion=1.19.1-SNAPSHOT
jetBrainsToken=invalid
jetBrainsChannel=stable
quarkusVersion=3.0.4.Final
lsp4mpVersion=0.7.1
quarkusLsVersion=0.14.1
kotlin.stdlib.default.dependency = false
18 changes: 18 additions & 0 deletions projects/lsp4mp/projects/maven/config-hover/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<version>1.0-SNAPSHOT</version>
<properties>
<compiler-plugin.version>3.8.1</compiler-plugin.version>
<lombok.version>1.18.26</lombok.version>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
Expand Down Expand Up @@ -48,6 +49,11 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-config-yaml</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -66,6 +72,18 @@
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${compiler-plugin.version}</version>
<configuration>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
<annotationProcessorPaths combine.children="append">
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class GreetingResource {
@ConfigProperty(name = "greeting.missing")
String missing;

/**
* The <code>number</code> of the greeting.
*/
@ConfigProperty(name = "greeting.number", defaultValue="0")
int number;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.acme.config;

public @interface MyAnnotation {

public static String MY_STRING = "asdf";

public String value() default MY_STRING;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.acme.config;

public enum MyEnum {

VALUE;

public static String MY_STRING = "asdf";

public void helloWorld();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.acme.config;

public interface MyInterface {

public static String MY_STRING = "asdf";

public void helloWorld();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.acme.config;

@Singleton
public class MyNestedClass {

@Singleton
static class MyNestedNestedClass {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.acme.config;

import lombok.NoArgsConstructor;

@NoArgsConstructor
public class WithLombok {

public String myField;

WithLombok(String myField) {
this.myField = myField;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.util.ThrowableComputable;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiDocumentManager;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.psi.PsiParameter;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.codelens.IJavaCodeLensParticipant;
import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.java.codelens.JavaCodeLensContext;
Expand All @@ -43,21 +36,12 @@
import org.eclipse.lsp4j.CodeLens;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4mp.commons.DocumentFormat;
import org.eclipse.lsp4mp.commons.JavaFileInfo;
import org.eclipse.lsp4mp.commons.MicroProfileDefinition;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCodeActionParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCodeLensParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaCompletionParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDefinitionParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaDiagnosticsSettings;
import org.eclipse.lsp4mp.commons.MicroProfileJavaFileInfoParams;
import org.eclipse.lsp4mp.commons.MicroProfileJavaHoverParams;
import org.eclipse.lsp4mp.commons.*;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -370,6 +354,195 @@ public Hover hover(MicroProfileJavaHoverParams params, IPsiUtils utils) {
});
}

/**
* Returns the cursor context for the given file and cursor position.
*
* @param params the completion params that provide the file and cursor
* position to get the context for
* @param utils the jdt utils
* @return the cursor context for the given file and cursor position
*/
public JavaCursorContextResult javaCursorContext(MicroProfileJavaCompletionParams params, IPsiUtils utils) {
String uri = params.getUri();
PsiFile typeRoot = resolveTypeRoot(uri, utils);
if (!(typeRoot instanceof PsiJavaFile)) {
return new JavaCursorContextResult(JavaCursorContextKind.IN_EMPTY_FILE, "");
}
Document document = PsiDocumentManager.getInstance(typeRoot.getProject()).getDocument(typeRoot);
if (document == null) {
return new JavaCursorContextResult(JavaCursorContextKind.IN_EMPTY_FILE, "");
}
Position completionPosition = params.getPosition();
int completionOffset = utils.toOffset(document, completionPosition.getLine(), completionPosition.getCharacter());

//CompilationUnit ast = ASTResolving.createQuickFixAST((ICompilationUnit) typeRoot);

JavaCursorContextKind kind = getJavaCursorContextKind((PsiJavaFile)typeRoot, completionOffset);
String prefix = getJavaCursorPrefix(document, completionOffset);

return new JavaCursorContextResult(kind, prefix);
}

private static @NotNull JavaCursorContextKind getJavaCursorContextKind(PsiJavaFile javaFile, int completionOffset) {
if (javaFile.getClasses().length == 0) {
return JavaCursorContextKind.IN_EMPTY_FILE;
}

PsiElement element = javaFile.findElementAt(completionOffset);
PsiElement parent = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class);

if (parent == null) {
// We are likely before or after the class declaration
PsiElement firstClass = javaFile.getClasses()[0];

if (completionOffset <= firstClass.getTextOffset()) {
return JavaCursorContextKind.BEFORE_CLASS;
}

return JavaCursorContextKind.NONE;
}

if (parent instanceof PsiClass) {
PsiClass psiClass = (PsiClass) parent;
return getContextKindFromClass(completionOffset, psiClass, element);
}
if (parent instanceof PsiAnnotation) {
PsiAnnotation psiAnnotation = (PsiAnnotation) parent;
@Nullable PsiAnnotationOwner annotationOwner = psiAnnotation.getOwner();
if (annotationOwner instanceof PsiClass) {
return (psiAnnotation.getStartOffsetInParent() == 0)? JavaCursorContextKind.BEFORE_CLASS:JavaCursorContextKind.IN_CLASS_ANNOTATIONS;
}
if (annotationOwner instanceof PsiMethod){
return (psiAnnotation.getStartOffsetInParent() == 0)? JavaCursorContextKind.BEFORE_METHOD:JavaCursorContextKind.IN_METHOD_ANNOTATIONS;
}
if (annotationOwner instanceof PsiField) {
return (psiAnnotation.getStartOffsetInParent() == 0)? JavaCursorContextKind.BEFORE_FIELD:JavaCursorContextKind.IN_FIELD_ANNOTATIONS;
}
}
if (parent instanceof PsiMethod) {
PsiMethod psiMethod = (PsiMethod) parent;
if (completionOffset == psiMethod.getTextRange().getStartOffset()) {
return JavaCursorContextKind.BEFORE_METHOD;
}
int methodStartOffset = getMethodStartOffset(psiMethod);
if (completionOffset <= methodStartOffset) {
if (psiMethod.getAnnotations().length > 0) {
return JavaCursorContextKind.IN_METHOD_ANNOTATIONS;
}
return JavaCursorContextKind.BEFORE_METHOD;
}
}

if (parent instanceof PsiField) {
PsiField psiField = (PsiField) parent;
if (completionOffset == psiField.getTextRange().getStartOffset()) {
return JavaCursorContextKind.BEFORE_FIELD;
}
int fieldStartOffset = getFieldStartOffset(psiField);
if (completionOffset <= fieldStartOffset) {
if (psiField.getAnnotations().length > 0) {
return JavaCursorContextKind.IN_FIELD_ANNOTATIONS;
}
return JavaCursorContextKind.BEFORE_FIELD;
}
}

return JavaCursorContextKind.NONE;
}

@NotNull
private static JavaCursorContextKind getContextKindFromClass(int completionOffset, PsiClass psiClass, PsiElement element) {
if (completionOffset <= psiClass.getTextRange().getStartOffset()) {
return JavaCursorContextKind.BEFORE_CLASS;
}
int classStartOffset = getClassStartOffset(psiClass);
if (completionOffset <= classStartOffset) {
if (psiClass.getAnnotations().length > 0) {
return JavaCursorContextKind.IN_CLASS_ANNOTATIONS;
}
return JavaCursorContextKind.BEFORE_CLASS;
}

PsiElement nextElement = element.getNextSibling();

if (nextElement instanceof PsiField) {
return JavaCursorContextKind.BEFORE_FIELD;
}
if (nextElement instanceof PsiMethod) {
return JavaCursorContextKind.BEFORE_METHOD;
}
if (nextElement instanceof PsiClass) {
return JavaCursorContextKind.BEFORE_CLASS;
}

return JavaCursorContextKind.IN_CLASS;
}

private static @NotNull String getJavaCursorPrefix(@NotNull Document document, int completionOffset) {
String fileContents = document.getText();
int i;
for (i = completionOffset; i > 0 && !Character.isWhitespace(fileContents.charAt(i - 1)); i--) {
}
return fileContents.substring(i, completionOffset);
}

private static int getMethodStartOffset(PsiMethod psiMethod) {
int startOffset = psiMethod.getTextOffset();

int modifierStartOffset = getFirstKeywordOffset(psiMethod);
if (modifierStartOffset > -1) {
return Math.min(startOffset, modifierStartOffset);
}

PsiTypeElement returnTypeElement = psiMethod.getReturnTypeElement();
if (returnTypeElement != null) {
int returnTypeEndOffset = returnTypeElement.getTextRange().getStartOffset();
startOffset = Math.min(startOffset, returnTypeEndOffset);
}

return startOffset;
}

private static int getClassStartOffset(PsiClass psiClass) {
int startOffset = psiClass.getTextOffset();

int modifierStartOffset = getFirstKeywordOffset(psiClass);
if (modifierStartOffset > -1) {
return Math.min(startOffset, modifierStartOffset);
}
return startOffset;
}

private static int getFieldStartOffset(PsiField psiField) {
int startOffset = psiField.getTextOffset();

int modifierStartOffset = getFirstKeywordOffset(psiField);
if (modifierStartOffset > -1) {
return Math.min(startOffset, modifierStartOffset);
}

PsiTypeElement typeElement = psiField.getTypeElement();
if (typeElement != null) {
int typeElementOffset = typeElement.getTextRange().getStartOffset();
startOffset = Math.min(startOffset, typeElementOffset);
}

return startOffset;
}

private static int getFirstKeywordOffset(PsiModifierListOwner modifierOwner) {
PsiModifierList modifierList = modifierOwner.getModifierList();
if (modifierList != null) {
PsiElement[] modifiers = modifierList.getChildren();
for (PsiElement modifier : modifiers) {
if (modifier instanceof PsiKeyword) {
return modifier.getTextRange().getStartOffset();
}
}
}
return -1;
}

@Nullable
private PsiElement getHoveredElement(PsiFile typeRoot, int offset) {
PsiElement hoverElement = typeRoot.findElementAt(offset);
Expand Down
Loading

0 comments on commit 0b6e94b

Please sign in to comment.