generated from redhat-developer/new-project-template
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
582 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# LSP client features | ||
|
||
The [LSPClientFeatures](https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/client/features/LSPClientFeatures.java) API allows customizing the behavior of LSP features to customize: | ||
|
||
* [LSP completion feature](#customize-lsp-completion-feature) | ||
* [LSP diagnostic feature](#customize-lsp-diagnostic-feature) | ||
* [LSP formatting feature](#customize-lsp-formatting-feature) | ||
|
||
These custom supports are done: | ||
|
||
* by extending the default classes `LSP*Feature` (e.g. creating a new class `MyLSPFormattingFeature` that extends | ||
[LSPFormattingFeature](https://github.com/redhat-developer/lsp4ij/blob/main/src/main/java/com/redhat/devtools/lsp4ij/client/features/LSPFormattingFeature.java) to customize formatting support) | ||
and overriding some methods to customize the behavior. | ||
* registering your custom classes with `LanguageServerFactory#createClientFeatures(@NotNull Project)`: | ||
|
||
```java | ||
package my.language.server; | ||
|
||
import com.intellij.openapi.project.Project; | ||
import com.redhat.devtools.lsp4ij.LanguageServerFactory; | ||
import com.redhat.devtools.lsp4ij.client.features.LSPClientFeatures; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class MyLanguageServerFactory implements LanguageServerFactory { | ||
|
||
@Override | ||
public @NotNull LSPClientFeatures createClientFeatures(@NotNull Project project) { | ||
return new LSPClientFeatures(project) | ||
.setCompletionFeature(new MyLSPCompletionFeature()) // customize LSP completion feature | ||
.setDiagnosticFeature(new MyLSPDiagnosticFeature()) // customize LSP diagnostic feature | ||
.setFormattingFeature(new MyLSPFormattingFeature()); // customize LSP formatting feature | ||
} | ||
} | ||
``` | ||
|
||
## Customize LSP completion feature | ||
|
||
TODO | ||
|
||
## Customize LSP diagnostic feature | ||
|
||
Here is an example of code that avoids creating an IntelliJ annotation when the LSP diagnostic code is equal to `ignore`: | ||
|
||
```java | ||
package my.language.server; | ||
|
||
import com.intellij.lang.annotation.HighlightSeverity; | ||
import com.redhat.devtools.lsp4ij.client.features.LSPDiagnosticFeature; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class MyLSPDiagnosticFeature extends LSPDiagnosticFeature { | ||
|
||
@Override | ||
public @Nullable HighlightSeverity getHighlightSeverity(@NotNull Diagnostic diagnostic) { | ||
if (diagnostic.getCode() != null && | ||
diagnostic.getCode().isLeft() && | ||
"ignore".equals(diagnostic.getCode().getLeft())) { | ||
// return a null HighlightSeverity when LSP diagnostic code is equals | ||
// to 'ignore' to avoid creating an IntelliJ annotation | ||
return null; | ||
} | ||
return super.getHighlightSeverity(diagnostic); | ||
} | ||
|
||
} | ||
``` | ||
|
||
## Customize LSP formatting feature | ||
|
||
Here is an example of code that allows to execute the LSP formatter even if there is a specific formatter registered by an IntelliJ plugin | ||
|
||
TODO: revisit this API to manage range formatting too. | ||
|
||
```java | ||
package my.language.server; | ||
|
||
import com.intellij.psi.PsiFile; | ||
import com.redhat.devtools.lsp4ij.client.features.LSPFormattingFeature; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class MyLSPFormattingFeature extends LSPFormattingFeature { | ||
|
||
@Override | ||
public boolean canSupportFormatting(@NotNull PsiFile file, boolean hasCustomFormatter) { | ||
// By default, LSPCustomizedFormattingSupport return false if it has custom formatter with psi | ||
// returns true even if there is custom formatter | ||
return true; | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/com/redhat/devtools/lsp4ij/client/features/AbstractLSPFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.client.features; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* Abstract class for any LSP feature. | ||
*/ | ||
@ApiStatus.Experimental | ||
public abstract class AbstractLSPFeature { | ||
|
||
private LSPClientFeatures clientFeatures; | ||
|
||
/** | ||
* Returns the LSP server support. | ||
* | ||
* @return the LSP server support. | ||
*/ | ||
public @NotNull LSPClientFeatures getClientFeatures() { | ||
return clientFeatures; | ||
} | ||
|
||
/** | ||
* Set the LSP server support. | ||
* | ||
* @param clientFeatures the LSP client features. | ||
*/ | ||
public void setClientFeatures(@NotNull LSPClientFeatures clientFeatures) { | ||
this.clientFeatures = clientFeatures; | ||
} | ||
|
||
} |
118 changes: 118 additions & 0 deletions
118
src/main/java/com/redhat/devtools/lsp4ij/client/features/LSPClientFeatures.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.client.features; | ||
|
||
import com.intellij.openapi.project.Project; | ||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* LSP client features. | ||
*/ | ||
@ApiStatus.Experimental | ||
public class LSPClientFeatures { | ||
|
||
private final @NotNull Project project; | ||
|
||
private LSPCompletionFeature completionFeature; | ||
|
||
private LSPDiagnosticFeature diagnosticFeature; | ||
|
||
private LSPFormattingFeature formattingFeature; | ||
|
||
public LSPClientFeatures(@NotNull Project project) { | ||
this.project = project; | ||
} | ||
|
||
/** | ||
* Returns the project. | ||
* | ||
* @return the project. | ||
*/ | ||
public @NotNull Project getProject() { | ||
return project; | ||
} | ||
|
||
/** | ||
* Returns the LSP completion feature. | ||
* | ||
* @return the LSP completion feature. | ||
*/ | ||
@NotNull | ||
public final LSPCompletionFeature getCompletionFeature() { | ||
if (completionFeature == null) { | ||
setCompletionFeature(new LSPCompletionFeature()); | ||
} | ||
return completionFeature; | ||
} | ||
|
||
/** | ||
* Initialize the LSP completion feature. | ||
* | ||
* @param completionFeature the LSP completion feature. | ||
* @return the LSP client features. | ||
*/ | ||
public final LSPClientFeatures setCompletionFeature(@NotNull LSPCompletionFeature completionFeature) { | ||
completionFeature.setClientFeatures(this); | ||
this.completionFeature = completionFeature; | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns the LSP diagnostic feature. | ||
* | ||
* @return the LSP diagnostic feature. | ||
*/ | ||
@NotNull | ||
public final LSPDiagnosticFeature getDiagnosticFeature() { | ||
if (diagnosticFeature == null) { | ||
setDiagnosticFeature(new LSPDiagnosticFeature()); | ||
} | ||
return diagnosticFeature; | ||
} | ||
|
||
/** | ||
* Initialize the LSP diagnostic feature. | ||
* | ||
* @param diagnosticFeature the LSP diagnostic feature. | ||
* @return the LSP client feature. | ||
*/ | ||
public LSPClientFeatures setDiagnosticFeature(@NotNull LSPDiagnosticFeature diagnosticFeature) { | ||
diagnosticFeature.setClientFeatures(this); | ||
this.diagnosticFeature = diagnosticFeature; | ||
return this; | ||
} | ||
|
||
/** | ||
* Returns the LSP formatting feature. | ||
* | ||
* @return the LSP formatting feature. | ||
*/ | ||
@NotNull | ||
public final LSPFormattingFeature getFormattingFeature() { | ||
if (formattingFeature == null) { | ||
setFormattingFeature(new LSPFormattingFeature()); | ||
} | ||
return formattingFeature; | ||
} | ||
|
||
/** | ||
* Initialize the LSP formatting feature. | ||
* | ||
* @param formattingFeature the LSP formatting feature. | ||
* @return the LSP client features. | ||
*/ | ||
public final LSPClientFeatures setFormattingFeature(@NotNull LSPFormattingFeature formattingFeature) { | ||
formattingFeature.setClientFeatures(this); | ||
this.formattingFeature = formattingFeature; | ||
return this; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/redhat/devtools/lsp4ij/client/features/LSPCompletionFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 Red Hat, Inc. | ||
* Distributed under license by Red Hat, Inc. All rights reserved. | ||
* This program is made available under the terms of the | ||
* Eclipse Public License v2.0 which accompanies this distribution, | ||
* and is available at https://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* Contributors: | ||
* Red Hat, Inc. - initial API and implementation | ||
******************************************************************************/ | ||
package com.redhat.devtools.lsp4ij.client.features; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
/** | ||
* LSP completion feature. | ||
*/ | ||
@ApiStatus.Experimental | ||
public class LSPCompletionFeature extends AbstractLSPFeature { | ||
|
||
} |
Oops, something went wrong.