public void TestCodeCompletionParser() { string code = @"x[y[z.foo()].goo()].bar"; string actual = CodeCompletionParser.GetStringToComplete(code); string expected = "x[y[z.foo()].goo()].bar"; Assert.AreEqual(expected, actual); code = @"abc.X[xyz.foo().Y"; actual = CodeCompletionParser.GetStringToComplete(code); expected = "xyz.foo().Y"; Assert.AreEqual(expected, actual); code = @"pnt[9][0] = abc.X[{xyz.b.foo((abc"; actual = CodeCompletionParser.GetStringToComplete(code); expected = "abc"; Assert.AreEqual(expected, actual); code = @"pnt[9][0] = abc.X[{xyz.b.foo((abc*x"; actual = CodeCompletionParser.GetStringToComplete(code); expected = "x"; Assert.AreEqual(expected, actual); code = @"w = abc; w = xyz; w = xyz.b; w = xyz.b.foo; w = xyz.b.foo.Y"; actual = CodeCompletionParser.GetStringToComplete(code); expected = "xyz.b.foo.Y"; Assert.AreEqual(expected, actual); }
private void OnTextAreaTextEntered(object sender, TextCompositionEventArgs e) { try { var code = this.InnerTextEditor.Text.Substring(0, this.InnerTextEditor.CaretOffset); if (e.Text == ".") { string stringToComplete = CodeCompletionParser.GetStringToComplete(code).Trim('.'); var completions = this.GetCompletionData(code, stringToComplete); if (!completions.Any()) { return; } ShowCompletionWindow(completions); } // Complete function signatures else if (e.Text == "(") { string functionName; string functionPrefix; CodeCompletionParser.GetFunctionToComplete(code, out functionName, out functionPrefix); var insightItems = this.GetFunctionSignatures(code, functionName, functionPrefix); ShowInsightWindow(insightItems); } else if (e.Text == ")") { if (insightWindow != null) { insightWindow.Close(); } } else if (completionWindow == null && (char.IsLetterOrDigit(e.Text[0]) || char.Equals(e.Text[0], '_'))) { // Autocomplete as you type // complete global methods (builtins), all classes, symbols local to codeblock node string stringToComplete = CodeCompletionParser.GetStringToComplete(code); var completions = this.SearchCompletions(stringToComplete, nodeModel.GUID); if (!completions.Any()) { return; } ShowCompletionWindow(completions, completeWhenTyping: true); } } catch (System.Exception ex) { this.dynamoViewModel.Model.Logger.Log("Failed to perform code block autocomplete with exception:"); this.dynamoViewModel.Model.Logger.Log(ex.Message); this.dynamoViewModel.Model.Logger.Log(ex.StackTrace); } }
private void OnTextAreaTextEntered(object sender, TextCompositionEventArgs e) { try { int startPos = this.InnerTextEditor.CaretOffset; var code = this.InnerTextEditor.Text.Substring(0, startPos); if (e.Text == ".") { if (CodeCompletionParser.IsInsideCommentOrString(code, startPos)) { return; } string stringToComplete = CodeCompletionParser.GetStringToComplete(code).Trim('.'); var completions = this.GetCompletionData(code, stringToComplete); if (completions == null || !completions.Any()) { return; } ShowCompletionWindow(completions); } // Complete function signatures else if (e.Text == "(") { if (CodeCompletionParser.IsInsideCommentOrString(code, startPos)) { return; } string functionName; string functionPrefix; CodeCompletionParser.GetFunctionToComplete(code, out functionName, out functionPrefix); var insightItems = this.GetFunctionSignatures(code, functionName, functionPrefix); ShowInsightWindow(insightItems); } else if (e.Text == ")") { if (insightWindow != null) { insightWindow.Close(); } } else if (completionWindow == null && (char.IsLetterOrDigit(e.Text[0]) || e.Text[0] == '_')) { // Begin completion while typing only if the previous character already typed in // is a white space or non-alphanumeric character if (startPos > 1 && char.IsLetterOrDigit(InnerTextEditor.Document.GetCharAt(startPos - 2))) { return; } if (CodeCompletionParser.IsInsideCommentOrString(code, startPos)) { return; } // Autocomplete as you type // complete global methods (builtins), all classes, symbols local to codeblock node string stringToComplete = CodeCompletionParser.GetStringToComplete(code); var completions = this.SearchCompletions(stringToComplete, nodeViewModel.NodeModel.GUID); if (!completions.Any()) { return; } ShowCompletionWindow(completions, completeWhenTyping: true); } } catch (Exception) { } }