Skip to content

Commit

Permalink
feat: Provide InlayHint for property expression in
Browse files Browse the repository at this point in the history
microprofile-config.properties (#971)

Fixes #971

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jun 27, 2023
1 parent 70cfefe commit 0828002
Show file tree
Hide file tree
Showing 20 changed files with 929 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,7 @@
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.redhat.devtools.intellij.lsp4ij.operations.diagnostics.LSPDiagnosticHandler;
import org.eclipse.lsp4j.ApplyWorkspaceEditParams;
import org.eclipse.lsp4j.ApplyWorkspaceEditResponse;
import org.eclipse.lsp4j.MessageActionItem;
import org.eclipse.lsp4j.MessageParams;
import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.RegistrationParams;
import org.eclipse.lsp4j.ShowMessageRequestParams;
import org.eclipse.lsp4j.UnregistrationParams;
import org.eclipse.lsp4j.WorkspaceFolder;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.LanguageServer;

Expand All @@ -30,6 +22,8 @@ public class LanguageClientImpl implements LanguageClient {

private boolean disposed;

private Runnable didChangeConfigurationListener;

public LanguageClientImpl(Project project) {
this.project = project;
}
Expand Down Expand Up @@ -109,4 +103,28 @@ public void dispose() {
public boolean isDisposed() {
return disposed;
}

protected Object createSettings() {
return null;
}

protected synchronized Runnable getDidChangeConfigurationListener() {
if (didChangeConfigurationListener != null) {
return didChangeConfigurationListener;
}
didChangeConfigurationListener = () -> {
LanguageServer languageServer = getLanguageServer();
if (languageServer == null) {
return;
}
Object settings = createSettings();
if(settings == null) {
return;
}
DidChangeConfigurationParams params = new DidChangeConfigurationParams(settings);
languageServer.getWorkspaceService().didChangeConfiguration(params);
};
return didChangeConfigurationListener;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* LSP textDocument/codeLens support.
*/
public class LSPCodelensInlayProvider extends AbstractLSPInlayProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(LSPCodelensInlayProvider.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import com.intellij.codeInsight.hints.InlayHintsSink;
import com.intellij.codeInsight.hints.NoSettings;
import com.intellij.codeInsight.hints.presentation.InlayPresentation;
import com.intellij.codeInsight.hints.presentation.MouseButton;
import com.intellij.codeInsight.hints.presentation.PresentationFactory;
import com.intellij.codeInsight.hints.presentation.SequencePresentation;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
Expand All @@ -27,21 +27,15 @@
import com.redhat.devtools.intellij.lsp4ij.AbstractLSPInlayProvider;
import com.redhat.devtools.intellij.lsp4ij.LSPIJUtils;
import com.redhat.devtools.intellij.lsp4ij.LanguageServiceAccessor;
import org.eclipse.lsp4j.InlayHint;
import org.eclipse.lsp4j.InlayHintLabelPart;
import org.eclipse.lsp4j.InlayHintParams;
import org.eclipse.lsp4j.InlayHintRegistrationOptions;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.TextDocumentIdentifier;
import org.eclipse.lsp4j.*;
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageServer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.Component;
import java.awt.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -54,6 +48,9 @@
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* LSP textDocument/inlayHint support.
*/
public class LSPInlayHintInlayProvider extends AbstractLSPInlayProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(LSPInlayHintInlayProvider.class);

Expand All @@ -69,10 +66,10 @@ public boolean collect(@NotNull PsiElement psiElement, @NotNull Editor editor, @
try {
URI docURI = LSPIJUtils.toUri(editor.getDocument());
if (docURI != null) {
Range viewPortRange = new Range(new Position(0, 0), new Position(0,0));
Range viewPortRange = getViewPortRange(editor);
InlayHintParams param = new InlayHintParams(new TextDocumentIdentifier(docURI.toString()), viewPortRange);
BlockingDeque<Pair<InlayHint, LanguageServer>> pairs = new LinkedBlockingDeque<>();
List<Pair<Integer,Pair<InlayHint, LanguageServer>>> inlayhints = new ArrayList<>();
List<Pair<Integer, Pair<InlayHint, LanguageServer>>> inlayhints = new ArrayList<>();
CompletableFuture<Void> future = LanguageServiceAccessor.getInstance(psiElement.getProject())
.getLanguageServers(editor.getDocument(), capabilities -> capabilities.getInlayHintProvider() != null)
.thenComposeAsync(languageServers -> CompletableFuture.allOf(languageServers.stream()
Expand All @@ -93,8 +90,8 @@ public boolean collect(@NotNull PsiElement psiElement, @NotNull Editor editor, @
inlayhints.add(Pair.create(offset, pair));
}
}
Map<Integer, List<Pair<Integer,Pair<InlayHint, LanguageServer>>>> elements = inlayhints.stream().collect(Collectors.groupingBy(p -> p.first));
elements.forEach((offset,list) -> inlayHintsSink.addInlineElement(offset, false,
Map<Integer, List<Pair<Integer, Pair<InlayHint, LanguageServer>>>> elements = inlayhints.stream().collect(Collectors.groupingBy(p -> p.first));
elements.forEach((offset, list) -> inlayHintsSink.addInlineElement(offset, false,
toPresentation(editor, offset, list, getFactory()), false));
}
} catch (InterruptedException e) {
Expand All @@ -106,6 +103,17 @@ public boolean collect(@NotNull PsiElement psiElement, @NotNull Editor editor, @
};
}

@NotNull
private static Range getViewPortRange(Editor editor) {
// LSP textDocument/inlayHnt request parameter expects to fill the visible view port range.
// As Intellij inlay hint provider is refreshed just only when editor is opened or editor content changed
// and not when editor is scrolling, the view port range must be created with full text document offsets.
Position start = new Position(0, 0);
Document document = editor.getDocument();
Position end = LSPIJUtils.toPosition(document.getTextLength(), document);
return new Range(start, end);
}

private InlayPresentation toPresentation(Editor editor, int offset,
List<Pair<Integer, Pair<InlayHint, LanguageServer>>> elements,
PresentationFactory factory) {
Expand All @@ -116,47 +124,50 @@ private InlayPresentation toPresentation(Editor editor, int offset,
presentations.add(factory.smallText(label.getLeft()));
} else {
int index = 0;
for(InlayHintLabelPart part : label.getRight()) {
InlayPresentation presentation = factory.smallText(part.getValue());
if (part.getCommand() != null) {
for (InlayHintLabelPart part : label.getRight()) {
InlayPresentation text = factory.smallText(part.getValue());
if (!hasCommand(part)) {
// No command, create a simple text inlay hint
presentations.add(text);
} else {
// InlayHintLabelPart defines a Command, create a clickable inlay hint
int finalIndex = index;
presentation = factory.onClick(presentation, MouseButton.Left, (event, point) -> {
text = factory.referenceOnHover(text, (event, translated) -> {
executeClientCommand(p.second.second, p.second.first, finalIndex, (Component) event.getSource(), editor.getProject());
return null;
});
if (part.getTooltip() != null && part.getTooltip().isLeft()) {
presentation = factory.withTooltip(part.getTooltip().getLeft(), presentation);
}
}
presentations.add(presentation);
if (part.getTooltip() != null && part.getTooltip().isLeft()) {
text = factory.withTooltip(part.getTooltip().getLeft(), text);
}
presentations.add(text);
index++;
}
}
});
return factory.roundWithBackground(new SequencePresentation(presentations));
}

private static boolean hasCommand(InlayHintLabelPart part) {
Command command = part.getCommand();
return (command != null && command.getCommand() != null && !command.getCommand().isEmpty());
}

private void executeClientCommand(LanguageServer languageServer, InlayHint inlayHint, int index, Component source,
Project project) {
if (LanguageServiceAccessor.getInstance(project).checkCapability(languageServer,
capabilites -> isResolveSupported(capabilites.getInlayHintProvider()))) {
languageServer.getTextDocumentService().resolveInlayHint(inlayHint).thenAcceptAsync(resolvedInlayHint -> {
executeClientCommand(source, resolvedInlayHint.getLabel().getRight().get(index).getCommand());
});
if (LanguageServiceAccessor.getInstance(project)
.checkCapability(languageServer, capabilites -> isResolveSupported(capabilites.getInlayHintProvider()))) {
languageServer.getTextDocumentService()
.resolveInlayHint(inlayHint)
.thenAcceptAsync(resolvedInlayHint -> {
executeClientCommand(source, resolvedInlayHint.getLabel().getRight().get(index).getCommand());
});
} else {
executeClientCommand(source, inlayHint.getLabel().getRight().get(index).getCommand());
}
}

private boolean isResolveSupported(Either<Boolean, InlayHintRegistrationOptions> provider) {
private static boolean isResolveSupported(Either<Boolean, InlayHintRegistrationOptions> provider) {
return provider.isRight() && provider.getRight().getResolveProvider();
}


private String getInlayHintString(InlayHint inlayHint) {
Either<String, List<InlayHintLabelPart>> label = inlayHint.getLabel();
return label.map(Function.identity(), parts -> {
return parts==null?null:parts.stream().map(InlayHintLabelPart::getValue).collect(Collectors.joining());
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.devtools.intellij.lsp4mp4ij;

import com.intellij.DynamicBundle;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.PropertyKey;

import java.util.function.Supplier;

/**
* MicroProfile messages bundle.
*/
public final class MicroProfileBundle extends DynamicBundle {

@NonNls public static final String BUNDLE = "messages.MicroProfileBundle";
private static final MicroProfileBundle INSTANCE = new MicroProfileBundle();

private MicroProfileBundle() {
super(BUNDLE);
}

@NotNull
public static @Nls String message(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object @NotNull ... params) {
return INSTANCE.getMessage(key, params);
}

@NotNull
public static Supplier<@Nls String> messagePointer(@NotNull @PropertyKey(resourceBundle = BUNDLE) String key, Object @NotNull ... params) {
return INSTANCE.getLazyMessage(key, params);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.intellij.util.ConcurrencyUtil;
import com.intellij.util.messages.MessageBusConnection;
import com.intellij.util.messages.Topic;
import com.intellij.workspaceModel.ide.WorkspaceModelTopics;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -48,7 +47,7 @@
*
*
* <ul>
* <li>Track update of libraries is done with {@link WorkspaceModelTopics}.
* <li>Track update of libraries is done with {@link com.intellij.openapi.roots.libraries.LibraryTable.Listener}.
* In other words {@link Listener#librariesChanged()} are fired when all libraries are inserted, deleted, updated.</li>
* <li>Track update of Java, microprofile-config properties files are done when Java Psi file is updated, when Java file is created, deleted, saved.</li>
* </ul>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.devtools.intellij.lsp4mp4ij.settings;

import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.ui.NamedConfigurable;
import com.intellij.openapi.util.NlsContexts;
import com.redhat.devtools.intellij.lsp4mp4ij.MicroProfileBundle;

import javax.swing.*;

/**
* MicroProfile configuration.
*/
public class MicroProfileConfigurable extends NamedConfigurable<UserDefinedMicroProfileSettings> {

private MicroProfileView myView;

public MicroProfileConfigurable() {
}

@Override
public UserDefinedMicroProfileSettings getEditableObject() {
return null;
}

@Override
public @NlsContexts.DetailedDescription String getBannerSlogan() {
return null;
}

@Override
public JComponent createOptionsPanel() {
if (myView == null) {
myView = new MicroProfileView();
}
return myView.getComponent();
}

@Override
public void setDisplayName(String name) {
}

@Override
public @NlsContexts.ConfigurableName String getDisplayName() {
return MicroProfileBundle.message("microprofile");
}


@Override
public boolean isModified() {
if (myView == null) return false;
return false;
}

@Override
public void apply() throws ConfigurationException {
// Do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* Copyright (c) 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package com.redhat.devtools.intellij.lsp4mp4ij.settings;

import com.intellij.openapi.options.Configurable;
import com.intellij.openapi.options.ConfigurableProvider;

/**
* MicroProfile UI settings provider.
*/
public class MicroProfileConfigurableProvider extends ConfigurableProvider {

@Override
public Configurable createConfigurable() {
return new MicroProfileConfigurable();
}

@Override
public boolean canCreateConfigurable() {
return true;
}

}
Loading

0 comments on commit 0828002

Please sign in to comment.