protected void Initialize() { // TODO: Unify getting auto complete data IEnumerable <CompletionData> completionData = null; int startPosition, wordLength = 0; string word = string.Empty; string filename = "filename.csx"; int offset = ScriptStart.Length; string newSourceCode = CorrectSource(""); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var resolver = unresolvedFile.GetResolver(compilation, location); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var completionDataFactory = new CompletionDataFactory(tooltipTextColor); var cce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpCompletionEngine(document, completionContextProvider, completionDataFactory, projectContent, typeResolveContextAtCaret); cce.EolMarker = Environment.NewLine; cce.FormattingPolicy = ICSharpCode.NRefactory.CSharp.FormattingOptionsFactory.CreateSharpDevelop(); var completionChar = document.GetCharAt(offset - 1); if (!cce.TryGetCompletionWord(offset, out startPosition, out wordLength)) { startPosition = offset; wordLength = 0; } completionData = cce.GetCompletionData(startPosition, true).Cast <CompletionData>(); if (wordLength > 0) { word = document.GetText(offset - wordLength, wordLength); } var parameterCompletionDataFactory = new ParameterCompletionDataFactory(tooltipTextColor); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(offset, completionChar); }
private void ShowAutoComplete(bool autoOpening, string text) { if (autoCompletePopup == null) { IEnumerable <CompletionData> completionData = null; int startPosition, wordLength = 0; string word = string.Empty; try { string filename = "filename.csx"; int offset = CaretOffset + ScriptStart.Length; string newSourceCode = CorrectSource(Text); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var resolver = unresolvedFile.GetResolver(compilation, location); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var completionDataFactory = new CompletionDataFactory(tooltipTextColor); var cce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpCompletionEngine(document, completionContextProvider, completionDataFactory, projectContent, typeResolveContextAtCaret); cce.EolMarker = Environment.NewLine; cce.FormattingPolicy = ICSharpCode.NRefactory.CSharp.FormattingOptionsFactory.CreateSharpDevelop(); var completionChar = document.GetCharAt(offset - 1); if (!autoOpening) { if (!cce.TryGetCompletionWord(offset, out startPosition, out wordLength)) { startPosition = offset; wordLength = 0; } completionData = cce.GetCompletionData(startPosition, true).Cast <CompletionData>(); } else { startPosition = offset; if (char.IsLetterOrDigit(completionChar) || completionChar == '_') { if (!(startPosition > 1 && char.IsLetterOrDigit(document.GetCharAt(startPosition - 2)))) { completionData = cce.GetCompletionData(startPosition, false).Cast <CompletionData>(); startPosition--; wordLength = 1; } } else { completionData = cce.GetCompletionData(startPosition, false).Cast <CompletionData>(); wordLength = 0; } } if (wordLength > 0) { word = document.GetText(offset - wordLength, wordLength); } if (functionCallPopup == null) { var parameterCompletionDataFactory = new ParameterCompletionDataFactory(tooltipTextColor); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(offset, completionChar); if (functionCallPopup == null && parameterDataProvider != null) { var ppd = parameterDataProvider as ParameterDataProvider; if (ppd.Methods.Length > 0) { functionCallPopup = new OverloadInsightWindow(TextArea); functionCallPopup.Provider = ppd; if (tooltipTextBackground != null) { functionCallPopup.Background = tooltipTextBackground; } if (tooltipTextColor != null) { functionCallPopup.Foreground = tooltipTextColor; } functionCallPopup.Show(); functionCallPopup.Closed += (o, args) => functionCallPopup = null; } return; } } } catch { } if (completionData != null && completionData.Any()) { autoCompletePopup = new CompletionWindow(TextArea); System.Windows.Shell.WindowChrome.SetWindowChrome( autoCompletePopup, new System.Windows.Shell.WindowChrome() { CaptionHeight = 0, }); if (completionTextBackground != null) { autoCompletePopup.CompletionList.ListBox.Background = completionTextBackground; } if (completionTextColor != null) { autoCompletePopup.CompletionList.ListBox.Foreground = completionTextColor; } autoCompletePopup.ResizeMode = System.Windows.ResizeMode.NoResize; autoCompletePopup.Width = 0; autoCompletePopup.MinWidth = 300; autoCompletePopup.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; autoCompletePopup.CloseWhenCaretAtBeginning = true; autoCompletePopup.StartOffset -= wordLength; var data = autoCompletePopup.CompletionList.CompletionData; if (completionData != null) { foreach (var cd in completionData.GroupBy(c => c.Content).Select(g => g.OrderBy(c => c.CompletionText).First()).OrderBy(c => c.Content)) { data.Add(cd); } if (wordLength > 0) { autoCompletePopup.CompletionList.SelectItem(word); } } autoCompletePopup.Show(); autoCompletePopup.Closed += (o, args) => autoCompletePopup = null; } } if (!string.IsNullOrEmpty(text) && functionCallPopup != null) { var provider = functionCallPopup.Provider as ParameterDataProvider; if (provider != null) { string filename = "filename.csx"; int offset = CaretOffset + ScriptStart.Length; string newSourceCode = CorrectSource(Text); var document = new ICSharpCode.NRefactory.Editor.ReadOnlyDocument(new ICSharpCode.NRefactory.Editor.StringTextSource(newSourceCode), filename); var syntaxTree = new ICSharpCode.NRefactory.CSharp.CSharpParser().Parse(document, document.FileName); syntaxTree.Freeze(); var unresolvedFile = syntaxTree.ToTypeSystem(); var projectContent = this.projectContent.AddOrUpdateFiles(unresolvedFile); var compilation = projectContent.CreateCompilation(); var location = document.GetLocation(offset); var typeResolveContextAtCaret = unresolvedFile.GetTypeResolveContext(compilation, location); var completionContextProvider = new ICSharpCode.NRefactory.CSharp.Completion.DefaultCompletionContextProvider(document, unresolvedFile); var parameterCompletionDataFactory = new ParameterCompletionDataFactory(tooltipTextColor); var completionChar = document.GetCharAt(offset - 1); var pce = new ICSharpCode.NRefactory.CSharp.Completion.CSharpParameterCompletionEngine( document, completionContextProvider, parameterCompletionDataFactory, projectContent, typeResolveContextAtCaret ); int parameterIndex = pce.GetCurrentParameterIndex(provider.StartOffset, offset); if (parameterIndex < 0) { functionCallPopup.Close(); functionCallPopup = null; } else { provider.CurrentParameter = parameterIndex - 1; } } } }