Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add snippet proposals loaded from templates file to language server. #2639

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion org.eclipse.xtext.ide.tests/build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ bin.includes = .,\
META-INF/,\
plugin.properties,\
about.html,\
plugin.xml
plugin.xml,\
templates/
source.. = src-gen,\
testlang-src,\
testlang-src-gen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public void testCompletion_01() {
testCompletion((TestCompletionConfiguration it) -> {
it.setModel("");
String expectedCompletionItems =
"type declaration template (Add type declaration) -> type ${1:name} {\n $0\n}\n [[0, 0] .. [0, 0]]\n" +
"package -> package [[0, 0] .. [0, 0]]\n" +
"type -> type [[0, 0] .. [0, 0]]\n" +
"Sample Snippet -> type ${1|A,B,C|} {\n" +
Expand Down Expand Up @@ -117,6 +118,7 @@ public void testSnippet() {
it.setLine(1);
it.setColumn(0);
String expectedCompletionItems =
"(Snippet|Snippet) type declaration template (Add type declaration) -> type ${1:name} {\n $0\n}\n [[1, 0] .. [1, 0]]\n" +
"(Keyword) package -> package [[1, 0] .. [1, 0]]\n" +
"(Keyword) type -> type [[1, 0] .. [1, 0]]\n" +
"(Snippet|Snippet) Sample Snippet -> type ${1|A,B,C|} {\n" +
Expand Down
13 changes: 13 additions & 0 deletions org.eclipse.xtext.ide.tests/templates/templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0"
encoding="UTF-8"
standalone="no"?>

<templates>
<template
context="org.eclipse.xtext.ide.tests.testlanguage.TestLanguage.TypeDeclaration"
description="Add type declaration"
name="type declaration template">type ${1:name} {
$0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my understanding: Does this template work in Eclipse, too? Because there I'd expect something like $cursor instead of $0

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe the syntax is the same, so I wouldn't expect it to work. These templates are based on the LSP snippet syntax https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#snippet_syntax

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so in xtext templates there is stuff like
${Name} ${public:Enum('Visibility')} ${event:CrossReference('Transition.event')}
i assume the first one you can easily adapt to lsp variant.
for the others i assume it needs more compilated calculation to achieve something like
https://github.com/itemis/xtext-languageserver-example/blob/dfbb365d70e1370db4d4b60bc193efa173082266/org.xtext.example.mydsl.ide/src/org/xtext/example/mydsl/ide/contentassist/MyDslIdeContentProposalProvider.java#L28

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the LSP snippet syntax, tab-stops are numbered, so ${Name} would become ${1:Name}. The others use JFace resolvers which I have assumed are not available on a server, so are not supported in this commit.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So yes, something more complicated is required to convert an enum resolver to a snippet choice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I'm not really a fan of the idea of duplicating the template definitions between LSP and Eclipse. I'd rather like to see templates being reused. Would you agree that the Eclipse templates could be translated into the LSP format while reading them from the XML file?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context, these are just the classes we have used for our project to get something working, and we thought they might be useful to you. If they go against your preferred implementation direction, then we will be fine to drop the PR.

To answer your question: Yes the xml files could be translated as they are read, but...

  • The obvious issue is that there is a performance hit to translating the xml file each time and we didn't want to take that hit when the result of the translation could be written out to a new file.
  • There are also variables that can be added to a snippet that are to be evaluated in the LSP client rather than the server - in a traditional Eclipse setup there would not be an LSP client to evaluate these.
  • Also, the syntax for a client-side-variable-with-default-value is the same as a jface-variable-with-no-argument-resolver, so a translator would need to know which case it was trying to translate.

}
</template>
</templates>
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
import org.eclipse.xtext.RuleCall;
import org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext;
import org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor;
import org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalProvider;
import org.eclipse.xtext.ide.editor.contentassist.TemplateIdeContentProposalProvider;
import org.eclipse.xtext.ide.tests.testlanguage.services.TestLanguageGrammarAccess;

import com.google.inject.Inject;

/**
* @author Christian Dietrich - Initial contribution and API
*/
public class TestLanguageIdeContentProposalProvider extends IdeContentProposalProvider {
public class TestLanguageIdeContentProposalProvider extends TemplateIdeContentProposalProvider {
@Inject
private TestLanguageGrammarAccess testLanguageGrammarAccess;

Expand All @@ -38,4 +38,9 @@ protected void _createProposals(RuleCall ruleCall, ContentAssistContext context,
}
super._createProposals(ruleCall, context, acceptor);
}

@Override
protected String getTemplateFilePlugin() {
return "org.eclipse.xtext.ide.tests";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2023 Avaloq Evolution AG (http://www.avaloq.com).
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.ide.editor.contentassist;

import org.eclipse.xtend2.lib.StringConcatenationClient;

import org.eclipse.xtext.ide.editor.contentassist.TemplateIdeContentProposalProvider.TemplateData;

import com.google.common.annotations.Beta;
import com.google.inject.Inject;

/**
* IdeTemplateProposalProvider that converts templates to snippet content assist proposals.
*
* @author Andrew Lamb - Initial contribution and API
*
* @since 2.31
*/
@Beta
public class SnippetTemplateProposalProvider extends AbstractIdeTemplateProposalProvider {
@Inject
private TemplateVariableProcessor variableResolver;

/**
* Create and accept a context assist proposal from the given template data.
*
* @param templateData
* the template data to create the proposal from.
* @param context
* the content assist context.
* @param acceptor
* the proposal acceptor.
*/
public void acceptProposal(final TemplateData templateData, final ContentAssistContext context,
final IIdeContentProposalAcceptor acceptor) {
StringConcatenationClient template = new StringConcatenationClient() {
@Override
protected void appendTo(final StringConcatenationClient.TargetStringConcatenation target) {
target.append(variableResolver.resolveAll(templateData.getPattern(), context));
}
};
acceptProposal(templateData.getName(), templateData.getDescription(), template, context, acceptor);
}

@Override
protected ContentAssistEntry createProposal(final StringConcatenationClient template,
final ContentAssistContext context, final boolean adaptIndentation) {
ContentAssistEntry entry = super.createProposal(template, context, adaptIndentation);
entry.setKind(ContentAssistEntry.KIND_SNIPPET);
return entry;
}

}
Loading