Skip to content

Commit 42d34a5

Browse files
committed
Quick and dirty fix of #190
1 parent 7ba9c07 commit 42d34a5

File tree

2 files changed

+77
-1
lines changed
  • headless-services

2 files changed

+77
-1
lines changed

headless-services/commons/commons-yaml/src/main/java/org/springframework/ide/vscode/commons/yaml/ast/YamlParser.java

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
package org.springframework.ide.vscode.commons.yaml.ast;
1313

14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
17+
import org.springframework.ide.vscode.commons.util.Assert;
1418
import org.springframework.ide.vscode.commons.util.text.IDocument;
1519
import org.yaml.snakeyaml.Yaml;
1620
import org.yaml.snakeyaml.nodes.Node;
@@ -27,9 +31,64 @@ public YamlParser() {
2731
@Override
2832
public YamlFileAST getAST(IDocument doc) throws Exception {
2933
CharSequenceReader reader = new CharSequenceReader();
30-
reader.setInput(doc.get());
34+
reader.setInput(atTokenTransformHack(doc.get()));
3135
Iterable<Node> nodes = new Yaml().composeAll(reader);
3236
return new YamlFileAST(doc, ImmutableList.copyOf(nodes));
3337
}
3438

39+
Pattern AT_TOKEN = Pattern.compile("^.*?(\\@[a-zA-z0-9_\\-\\.]*\\@).*?$", Pattern.MULTILINE);
40+
41+
static class StringCopier {
42+
private int offset = 0;
43+
private String input;
44+
private StringBuilder output;
45+
46+
public StringCopier(String input) {
47+
this.input = input;
48+
this.output = new StringBuilder();
49+
}
50+
51+
public void copyUpto(int upto) {
52+
if (upto>offset) {
53+
output.append(input.substring(offset, upto));
54+
offset = upto;
55+
}
56+
}
57+
58+
public void replace(int len) {
59+
if (len>=3) {
60+
offset+=len;
61+
len-=2;
62+
output.append('"');
63+
for (int i = 0; i < len; i++) {
64+
output.append('@');
65+
}
66+
output.append('"');
67+
} else {
68+
copyUpto(offset+len);
69+
}
70+
}
71+
}
72+
73+
private CharSequence atTokenTransformHack(String input) {
74+
Matcher matcher = AT_TOKEN.matcher(input);
75+
StringCopier transformed = new StringCopier(input);
76+
while (matcher.find()) {
77+
int lineStart = matcher.start();
78+
79+
int tokenStart = matcher.start(1);
80+
int tokenEnd = matcher.end(1);
81+
82+
String pre = input.substring(lineStart, tokenStart);
83+
if (!pre.contains("\"")) {
84+
transformed.copyUpto(tokenStart);
85+
transformed.replace(tokenEnd-tokenStart);
86+
}
87+
}
88+
transformed.copyUpto(input.length());
89+
String out = transformed.output.toString();
90+
Assert.isTrue(out.length()==input.length());
91+
return out;
92+
}
93+
3594
}

headless-services/spring-boot-language-server/src/test/java/org/springframework/ide/vscode/boot/test/ApplicationYamlEditorTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,23 @@ public void cleanups() {
9090

9191
////////////////////////////////////////////////////////////////////////////////////////
9292

93+
@Test public void GH_190_tolerate_placeholders_without_quotes() throws Exception {
94+
//See: https://github.com/spring-projects/sts4/issues/190
95+
data("info.build", "java.util.Map<String,String>", null, null);
96+
Editor editor = harness.newEditor(
97+
"info:\n" +
98+
" build:\n" +
99+
" artifact: @project.artifactId@\n" +
100+
" name: @project.name@\n" +
101+
" description: @project.description@\n" +
102+
" version: @project.version@\n" +
103+
"bad: problem"
104+
);
105+
editor.assertProblems(
106+
"bad|Unknown"
107+
);
108+
}
109+
93110
@Test public void GH_449_inheritedPropertiesInListValues() throws Exception {
94111
//See: https://github.com/spring-projects/sts4/issues/449
95112
MavenJavaProject p = createPredefinedMavenProject("gh_449");

0 commit comments

Comments
 (0)