Skip to content

Commit 43eae98

Browse files
committed
Merge pull request #464 from mousetraps/i462
Fix #462 completions should not be displayed after funciton, let, const
2 parents 33ac229 + f1cad85 commit 43eae98

File tree

2 files changed

+64
-57
lines changed

2 files changed

+64
-57
lines changed

Nodejs/Product/Nodejs/Intellisense/VsProjectAnalyzer.cs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ sealed partial class VsProjectAnalyzer : IDisposable {
9191

9292
private readonly TaskProvider _defaultTaskProvider = CreateDefaultTaskProvider();
9393

94+
internal static readonly string[] _emptyCompletionContextKeywords = new string[] {
95+
"var", "function", "const", "let"
96+
};
9497
#if FALSE
9598
private readonly UnresolvedImportSquiggleProvider _unresolvedSquiggles;
9699
#endif
@@ -156,9 +159,9 @@ private void CreateNewAnalyzer(AnalysisLimits limits) {
156159
_fullyLoaded = true;
157160
}
158161

159-
private bool ShouldEnqueue() {
160-
return _analysisLevel != AnalysisLevel.None && _analysisLevel != AnalysisLevel.Preview;
161-
}
162+
private bool ShouldEnqueue() {
163+
return _analysisLevel != AnalysisLevel.None && _analysisLevel != AnalysisLevel.Preview;
164+
}
162165

163166
#region Public API
164167

@@ -257,7 +260,7 @@ public void RemoveBuffer(ITextBuffer buffer) {
257260
}
258261

259262
BufferParser bufferParser;
260-
if (!buffer.Properties.TryGetProperty<BufferParser>(typeof(BufferParser), out bufferParser)) {
263+
if (!buffer.Properties.TryGetProperty<BufferParser>(typeof(BufferParser), out bufferParser)) {
261264
return;
262265
}
263266

@@ -323,7 +326,7 @@ private void AnalyzeFile(string path, bool reportErrors, ProjectItem originating
323326
if (!reportErrors) {
324327
TaskProvider.Clear(item.Entry, ParserTaskMoniker);
325328
}
326-
329+
327330
if ((_reparseDateTime != null && new FileInfo(path).LastWriteTime > _reparseDateTime.Value)
328331
|| (reportErrors && !item.ReportErrors) ||
329332
(item.Entry.Module == null || item.Entry.Unit == null)) {
@@ -378,21 +381,21 @@ public void AddPackageJson(string packageJsonPath) {
378381
AddPackageJson(packageJsonPath, (string)mainFile, dependencyList);
379382
}
380383
}
381-
}
382-
383-
private static List<string> GetDependencyListFromJson(Dictionary<string, object> json, params string[] dependencyTypes) {
384-
var allDependencies = new List<string>();
385-
foreach (var type in dependencyTypes) {
386-
object dependencies;
387-
json.TryGetValue(type, out dependencies);
388-
var dep = dependencies as Dictionary<string, object>;
389-
if (dep != null) {
390-
allDependencies.AddRange(dep.Keys.ToList());
391-
}
392-
}
393-
return allDependencies;
394-
}
395-
384+
}
385+
386+
private static List<string> GetDependencyListFromJson(Dictionary<string, object> json, params string[] dependencyTypes) {
387+
var allDependencies = new List<string>();
388+
foreach (var type in dependencyTypes) {
389+
object dependencies;
390+
json.TryGetValue(type, out dependencies);
391+
var dep = dependencies as Dictionary<string, object>;
392+
if (dep != null) {
393+
allDependencies.AddRange(dep.Keys.ToList());
394+
}
395+
}
396+
return allDependencies;
397+
}
398+
396399
public void AddPackageJson(string path, string mainFile, List<string> dependencies) {
397400
if (!_fullyLoaded) {
398401
lock (_loadingDeltas) {
@@ -1191,17 +1194,17 @@ private static CompletionAnalysis TrySpecialCompletions(ITextSnapshot snapshot,
11911194
if (range != null) {
11921195
start = range.Value.Start;
11931196
}
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,
1197+
}
1198+
1199+
// Get the classifiers from beginning of the line to the beginning of snapSpan.
1200+
// The contents of snapSpan differ depending on what is determined in
1201+
// CompletionSource.GetApplicableSpan.
1202+
//
1203+
// In the case of:
1204+
// var myIdentifier<cursor>
1205+
// the applicable span will be "myIdentifier", so GetClassificationSpans will operate on "var "
1206+
//
1207+
// In the case of comments and string literals, the applicable span will be empty,
12051208
// so snapSpan.Start will occur at the current cursor position.
12061209
var tokens = classifier.GetClassificationSpans(new SnapshotSpan(start.GetContainingLine().Start, snapSpan.Start));
12071210
if (tokens.Count > 0) {
@@ -1210,8 +1213,9 @@ private static CompletionAnalysis TrySpecialCompletions(ITextSnapshot snapshot,
12101213

12111214
if (lastClass.ClassificationType == classifier.Provider.Comment ||
12121215
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.
1216+
(lastClass.ClassificationType == classifier.Provider.Keyword &&
1217+
_emptyCompletionContextKeywords.Contains(lastClass.Span.GetText()))) {
1218+
// No completions in comments, strings, or directly after certain keywords.
12151219
return CompletionAnalysis.EmptyCompletionContext;
12161220
}
12171221
return null;
@@ -1288,7 +1292,7 @@ private void OnErrorRemoved(string path) {
12881292
private void ClearParserTasks(IProjectEntry entry) {
12891293
if (entry != null) {
12901294
TaskProvider.Clear(entry, ParserTaskMoniker);
1291-
1295+
12921296
bool changed;
12931297
lock (_hasParseErrors) {
12941298
changed = _hasParseErrors.Remove(entry);

Nodejs/Tests/Core.UI/BasicIntellisense.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using System;
1818
using System.Linq;
1919
using System.Windows;
20+
using Microsoft.NodejsTools.Intellisense;
2021
using Microsoft.VisualStudio.Language.Intellisense;
2122
using Microsoft.VisualStudio.TestTools.UnitTesting;
2223
using Microsoft.VisualStudio.Text;
@@ -272,29 +273,31 @@ public void IntellisenseAfterMultiLineComment() {
272273
/// </summary>
273274
[TestMethod, Priority(0), TestCategory("Core")]
274275
[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)) { }
276+
public void IntellisenseAfterEmptyCompletionContextKeywords() {
277+
foreach (var keyword in VsProjectAnalyzer._emptyCompletionContextKeywords) {
278+
var project = Project("IntellisenseAfterEmptyCompletionContextKeywordTest",
279+
Compile("server", String.Format("{0} c \r\nexports.{0} = 3; exports.{0} ", keyword))
280+
);
281+
282+
using (var solution = project.Generate().ToVs()) {
283+
var server = solution.OpenItem("IntellisenseAfterEmptyCompletionContextKeywordTest", "server.js");
284+
285+
server.MoveCaret(1, keyword.Length + 1);
286+
Keyboard.Type(Keyboard.CtrlSpace.ToString());
287+
using (var sh = server.WaitForSession<ICompletionSession>(true)) { }
288+
289+
server.MoveCaret(1, keyword.Length + 3);
290+
Keyboard.Type(Keyboard.CtrlSpace.ToString());
291+
server.AssertNoIntellisenseSession();
292+
293+
server.MoveCaret(1, keyword.Length + 4);
294+
Keyboard.Type(Keyboard.CtrlSpace.ToString());
295+
using (var sh = server.WaitForSession<ICompletionSession>(true)) { }
296+
297+
server.MoveCaret(2, keyword.Length*2 + 24);
298+
Keyboard.Type(Keyboard.CtrlSpace.ToString());
299+
using (var sh = server.WaitForSession<ICompletionSession>(true)) { }
300+
}
298301
}
299302
}
300303

0 commit comments

Comments
 (0)