-
Notifications
You must be signed in to change notification settings - Fork 321
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
andrewL-avlq
wants to merge
1
commit into
eclipse-xtext:main
Choose a base branch
from
andrewL-avlq:ContentAssistTemplates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 | ||
} | ||
</template> | ||
</templates> |
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
59 changes: 59 additions & 0 deletions
59
...t.ide/src/org/eclipse/xtext/ide/editor/contentassist/SnippetTemplateProposalProvider.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,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; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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...