Skip to content

Commit cc0f4e2

Browse files
committed
fix: Variable is not supported in a snippet placeholder
Fixes #1313 Signed-off-by: azerr <azerr@redhat.com>
1 parent c1b6d8d commit cc0f4e2

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/main/java/com/redhat/devtools/lsp4ij/client/features/LSPCompletionProposal.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,10 @@ public CompletionItem getItem() {
435435
* @return the response of the resolved LSP variable and null otherwise.
436436
*/
437437
private @Nullable String getVariableValue(String variableName) {
438+
if (!variableName.isEmpty() && variableName.charAt(0) == '$') {
439+
// Remove $
440+
variableName = variableName.substring(1);
441+
}
438442
Document document = editor.getDocument();
439443
switch (variableName) {
440444
case TM_FILENAME_BASE:
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package com.redhat.devtools.lsp4ij.features.completion;
12+
13+
import com.redhat.devtools.lsp4ij.fixtures.LSPCompletionFixtureTestCase;
14+
15+
/**
16+
* Completion tests with 'textDocument/completion' responses
17+
* which returns snippets with LSP standard variables}.
18+
*
19+
* @see {@link com.redhat.devtools.lsp4ij.features.completion.snippet.LspSnippetVariableConstants}
20+
*/
21+
public class VariableCompletionTest extends LSPCompletionFixtureTestCase {
22+
23+
public VariableCompletionTest() {
24+
super("*.ts");
25+
}
26+
27+
public void testCompletionWith$TM_FILENAME() {
28+
// 1. Test completion items response
29+
assertCompletion("MyComponent.ts",
30+
"<caret>", // language=json
31+
"""
32+
33+
{
34+
"isIncomplete": false,
35+
"items": [
36+
{
37+
"label": "Type",
38+
"kind": 15,
39+
"insertTextFormat": 2,
40+
"insertTextMode": 2,
41+
"textEdit": {
42+
"range": {
43+
"start": {
44+
"line": 1,
45+
"character": 1
46+
},
47+
"end": {
48+
"line": 1,
49+
"character": 1
50+
}
51+
},
52+
"newText": "Type ${1:$TM_FILENAME} {\\n |\\n}"
53+
}
54+
}
55+
]
56+
}"""
57+
,
58+
"Type");
59+
// 2. Test new editor content after applying the first completion item
60+
61+
// Apply completion with Type
62+
assertApplyCompletionItem(0,
63+
"""
64+
Type MyComponent.ts {
65+
|
66+
}""");
67+
}
68+
69+
public void testCompletionWith$TM_FILENAME_BASE() {
70+
// 1. Test completion items response
71+
assertCompletion("MyComponent.ts",
72+
"<caret>", // language=json
73+
"""
74+
75+
{
76+
"isIncomplete": false,
77+
"items": [
78+
{
79+
"label": "Type",
80+
"kind": 15,
81+
"insertTextFormat": 2,
82+
"insertTextMode": 2,
83+
"textEdit": {
84+
"range": {
85+
"start": {
86+
"line": 1,
87+
"character": 1
88+
},
89+
"end": {
90+
"line": 1,
91+
"character": 1
92+
}
93+
},
94+
"newText": "Type ${1:$TM_FILENAME_BASE} {\\n |\\n}"
95+
}
96+
}
97+
]
98+
}"""
99+
,
100+
"Type");
101+
// 2. Test new editor content after applying the first completion item
102+
103+
// Apply completion with Type
104+
assertApplyCompletionItem(0,
105+
"""
106+
Type MyComponent {
107+
|
108+
}""");
109+
}
110+
}

0 commit comments

Comments
 (0)