Skip to content

Commit 026050a

Browse files
committed
Merge pull request #456 from mousetraps/i454
Fix #454 Don't show completions after "var" keyword
2 parents 0139fe8 + 42da89e commit 026050a

2 files changed

Lines changed: 47 additions & 8 deletions

File tree

Nodejs/Product/Nodejs/Intellisense/VsProjectAnalyzer.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,18 +1191,27 @@ private static CompletionAnalysis TrySpecialCompletions(ITextSnapshot snapshot,
11911191
if (range != null) {
11921192
start = range.Value.Start;
11931193
}
1194-
}
1195-
1194+
}
1195+
1196+
// Get the classifiers from beginning of the line to the beginning of snapSpan.
1197+
// The contents of snapSpan differ depending on what is determined in
1198+
// CompletionSource.GetApplicableSpan.
1199+
//
1200+
// In the case of:
1201+
// var myIdentifier<cursor>
1202+
// the applicable span will be "myIdentifier", so GetClassificationSpans will operate on "var "
1203+
//
1204+
// In the case of comments and string literals, the applicable span will be empty,
1205+
// so snapSpan.Start will occur at the current cursor position.
11961206
var tokens = classifier.GetClassificationSpans(new SnapshotSpan(start.GetContainingLine().Start, snapSpan.Start));
11971207
if (tokens.Count > 0) {
11981208
// Check for context-sensitive intellisense
11991209
var lastClass = tokens[tokens.Count - 1];
12001210

1201-
if (lastClass.ClassificationType == classifier.Provider.Comment) {
1202-
// No completions in comments
1203-
return CompletionAnalysis.EmptyCompletionContext;
1204-
} else if (lastClass.ClassificationType == classifier.Provider.StringLiteral) {
1205-
// String completion
1211+
if (lastClass.ClassificationType == classifier.Provider.Comment ||
1212+
lastClass.ClassificationType == classifier.Provider.StringLiteral ||
1213+
(lastClass.ClassificationType == classifier.Provider.Keyword && lastClass.Span.GetText() == "var")) {
1214+
// No completions in comments, strings, or directly after "var" keywords.
12061215
return CompletionAnalysis.EmptyCompletionContext;
12071216
}
12081217
return null;

Nodejs/Tests/Core.UI/BasicIntellisense.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ public void CtrlSpace() {
4343
var server = solution.OpenItem("CtrlSpace", "server.js");
4444

4545
server.MoveCaret(2, 13);
46-
4746
solution.ExecuteCommand("Edit.CompleteWord");
4847

4948
server.WaitForText("var http = require('http');\r\nhttp.createServer");
@@ -268,6 +267,37 @@ public void IntellisenseAfterMultiLineComment() {
268267
}
269268
}
270269

270+
/// <summary>
271+
/// https://github.com/Microsoft/nodejstools/issues/454
272+
/// </summary>
273+
[TestMethod, Priority(0), TestCategory("Core")]
274+
[HostType("VSTestHost")]
275+
public void IntellisenseAfterVarKeyword() {
276+
var project = Project("IntellisenseAfterVarKeywordTest",
277+
Compile("server", "var c \r\nexports.var = 3; exports.var ")
278+
);
279+
280+
using (var solution = project.Generate().ToVs()) {
281+
var server = solution.OpenItem("IntellisenseAfterVarKeywordTest", "server.js");
282+
283+
server.MoveCaret(1, 4);
284+
Keyboard.Type(Keyboard.CtrlSpace.ToString());
285+
using (var sh = server.WaitForSession<ICompletionSession>(true)) { }
286+
287+
server.MoveCaret(1, 6);
288+
Keyboard.Type(Keyboard.CtrlSpace.ToString());
289+
server.AssertNoIntellisenseSession();
290+
291+
server.MoveCaret(1, 7);
292+
Keyboard.Type(Keyboard.CtrlSpace.ToString());
293+
using (var sh = server.WaitForSession<ICompletionSession>(true)) { }
294+
295+
server.MoveCaret(2, 30);
296+
Keyboard.Type(Keyboard.CtrlSpace.ToString());
297+
using (var sh = server.WaitForSession<ICompletionSession>(true)) { }
298+
}
299+
}
300+
271301
[TestMethod, Priority(0), TestCategory("Core")]
272302
[HostType("VSTestHost")]
273303
public void JSDocTest() {

0 commit comments

Comments
 (0)