void window_CaretPositionChanged(object sender, EventArgs e) { var completionContext = CSharpCompletionContext.Get(editor); if (completionContext == null) { window.Close(); return; } var completionFactory = new CSharpCompletionDataFactory(completionContext, new CSharpResolver(completionContext.TypeResolveContextAtCaret)); var pce = new CSharpParameterCompletionEngine( editor.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); UpdateHighlightedParameter(pce); }
public void UpdateHighlightedParameter(CSharpParameterCompletionEngine pce) { int parameterIndex = pce.GetCurrentParameterIndex(window != null ? window.StartOffset : startOffset, editor.Caret.Offset); if (parameterIndex < 0 && window != null) { window.Close(); } else { if (window == null || parameterIndex > ((CSharpInsightItem)window.SelectedItem).Method.Parameters.Count) { var newItem = items.FirstOrDefault(i => parameterIndex <= i.Method.Parameters.Count); if (newItem != null) { if (window != null) window.SelectedItem = newItem; else initiallySelectedItem = newItem; } } if (parameterIndex > 0) parameterIndex--; // NR returns 1-based parameter index foreach (var item in items) item.HighlightParameter(parameterIndex); } }
static int GetIndex(string text) { var editorText = new StringBuilder(); int trigger = 0, end = 0; for (int i = 0; i < text.Length; i++) { if (text[i] == '@') { trigger = editorText.Length; continue; } if (text[i] == '$') { end = editorText.Length; continue; } editorText.Append(text [i]); } var doc = new ReadOnlyDocument(editorText.ToString ()); var pctx = new CSharpProjectContent(); var rctx = new CSharpTypeResolveContext(pctx.CreateCompilation().MainAssembly); var ctxProvider = new DefaultCompletionContextProvider(doc, new CSharpUnresolvedFile()); var engine = new CSharpParameterCompletionEngine(doc, ctxProvider, new ParameterCompletionTests.TestFactory(pctx), pctx, rctx); return engine.GetCurrentParameterIndex(trigger, end); }
public CodeCompletionResult GetCompletions(IDocument document, int offset, bool controlSpace, string usings, string variables) { var result = new CodeCompletionResult(); if (String.IsNullOrEmpty(document.FileName)) return result; var completionContext = new CSharpCompletionContext(document, offset, projectContent, usings, variables); var completionFactory = new CSharpCompletionDataFactory(completionContext.TypeResolveContextAtCaret, completionContext); var cce = new CSharpCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); cce.EolMarker = Environment.NewLine; cce.FormattingPolicy = FormattingOptionsFactory.CreateSharpDevelop(); var completionChar = completionContext.Document.GetCharAt(completionContext.Offset - 1); int startPos, triggerWordLength; IEnumerable<ICSharpCode.NRefactory.Completion.ICompletionData> completionData; if (controlSpace) { if (!cce.TryGetCompletionWord(completionContext.Offset, out startPos, out triggerWordLength)) { startPos = completionContext.Offset; triggerWordLength = 0; } completionData = cce.GetCompletionData(startPos, true); //this outputs tons of available entities //if (triggerWordLength == 0) // completionData = completionData.Concat(cce.GetImportCompletionData(startPos)); } else { startPos = completionContext.Offset; if (char.IsLetterOrDigit(completionChar) || completionChar == '_') { if (startPos > 1 && char.IsLetterOrDigit(completionContext.Document.GetCharAt(startPos - 2))) return result; completionData = cce.GetCompletionData(startPos, false); startPos--; triggerWordLength = 1; } else { completionData = cce.GetCompletionData(startPos, false); triggerWordLength = 0; } } result.TriggerWordLength = triggerWordLength; result.TriggerWord = completionContext.Document.GetText(completionContext.Offset - triggerWordLength, triggerWordLength); Debug.Print("Trigger word: '{0}'", result.TriggerWord); //cast to AvalonEdit completion data and add to results foreach (var completion in completionData) { var cshellCompletionData = completion as ICSharpCode.CodeCompletion.DataItems.CompletionData; if (cshellCompletionData != null) { cshellCompletionData.TriggerWord = result.TriggerWord; cshellCompletionData.TriggerWordLength = result.TriggerWordLength; result.CompletionData.Add(cshellCompletionData); } } //method completions if (!controlSpace) { // Method Insight var pce = new CSharpParameterCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(completionContext.Offset, completionChar); result.OverloadProvider = parameterDataProvider as IOverloadProvider; } return result; }
bool ShowCompletion(ITextEditor editor, char completionChar, bool ctrlSpace) { var completionContext = CSharpCompletionContext.Get(editor); if (completionContext == null) return false; var completionFactory = new CSharpCompletionDataFactory(completionContext, new CSharpResolver(completionContext.TypeResolveContextAtCaret)); CSharpCompletionEngine cce = new CSharpCompletionEngine( editor.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); cce.FormattingPolicy = FormattingOptionsFactory.CreateSharpDevelop(); cce.EolMarker = DocumentUtilities.GetLineTerminator(editor.Document, editor.Caret.Line); cce.IndentString = editor.Options.IndentationString; int startPos, triggerWordLength; IEnumerable<ICompletionData> completionData; if (ctrlSpace) { if (!cce.TryGetCompletionWord(editor.Caret.Offset, out startPos, out triggerWordLength)) { startPos = editor.Caret.Offset; triggerWordLength = 0; } completionData = cce.GetCompletionData(startPos, true); completionData = completionData.Concat(cce.GetImportCompletionData(startPos)); } else { startPos = editor.Caret.Offset; if (char.IsLetterOrDigit (completionChar) || completionChar == '_') { if (startPos > 1 && char.IsLetterOrDigit (editor.Document.GetCharAt (startPos - 2))) return false; completionData = cce.GetCompletionData(startPos, false); startPos--; triggerWordLength = 1; } else { completionData = cce.GetCompletionData(startPos, false); triggerWordLength = 0; } } DefaultCompletionItemList list = new DefaultCompletionItemList(); list.Items.AddRange(completionData.Cast<ICompletionItem>()); if (list.Items.Count > 0) { list.SortItems(); list.PreselectionLength = editor.Caret.Offset - startPos; list.PostselectionLength = Math.Max(0, startPos + triggerWordLength - editor.Caret.Offset); list.SuggestedItem = list.Items.FirstOrDefault(i => i.Text == cce.DefaultCompletionString); editor.ShowCompletionWindow(list); return true; } if (!ctrlSpace) { // Method Insight var pce = new CSharpParameterCompletionEngine( editor.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); var newInsight = pce.GetParameterDataProvider(editor.Caret.Offset, completionChar) as CSharpMethodInsight; if (newInsight != null && newInsight.items.Count > 0) { newInsight.UpdateHighlightedParameter(pce); newInsight.Show(); return true; } } return false; }
public void Update(IDocument document, int offset) { var completionContext = new CSharpCompletionContext(document, offset, context.ProjectContent); if (completionContext == null) { RequestClose = true; return; } var completionFactory = new CSharpCompletionDataFactory(completionContext.TypeResolveContextAtCaret, completionContext); var pce = new CSharpParameterCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); var completionChar = completionContext.Document.GetCharAt(completionContext.Offset); int parameterIndex = pce.GetCurrentParameterIndex(startOffset, completionContext.Offset); if (parameterIndex < 0) { RequestClose = true; return; } else { if (parameterIndex > items[selectedIndex].Method.Parameters.Count) { var newItem = items.FirstOrDefault(i => parameterIndex <= i.Method.Parameters.Count); SelectedIndex = items.IndexOf(newItem); } if (parameterIndex > 0) parameterIndex--; // NR returns 1-based parameter index foreach (var item in items) { item.HighlightParameter(parameterIndex); } } }
internal static IParameterDataProvider CreateProvider (string text) { string parsedText; string editorText; int cursorPosition = text.IndexOf ('$'); int endPos = text.IndexOf ('$', cursorPosition + 1); if (endPos == -1) parsedText = editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1); else { parsedText = text.Substring (0, cursorPosition) + new string (' ', endPos - cursorPosition) + text.Substring (endPos + 1); editorText = text.Substring (0, cursorPosition) + text.Substring (cursorPosition + 1, endPos - cursorPosition - 1) + text.Substring (endPos + 1); cursorPosition = endPos - 1; } var doc = new ReadOnlyDocument (editorText); IProjectContent pctx = new CSharpProjectContent (); pctx = pctx.AddAssemblyReferences (new [] { CecilLoaderTests.Mscorlib, CecilLoaderTests.SystemCore }); var compilationUnit = new CSharpParser ().Parse (parsedText, "program.cs"); var parsedFile = compilationUnit.ToTypeSystem (); pctx = pctx.UpdateProjectContent (null, parsedFile); var cmp = pctx.CreateCompilation (); var loc = doc.GetLocation (cursorPosition); var engine = new CSharpParameterCompletionEngine (doc, new TestFactory (pctx)); var rctx = new CSharpTypeResolveContext (cmp.MainAssembly); rctx = rctx.WithUsingScope (parsedFile.GetUsingScope (loc).Resolve (cmp)); var curDef = parsedFile.GetInnermostTypeDefinition (loc); if (curDef != null) { rctx = rctx.WithCurrentTypeDefinition (curDef.Resolve (rctx).GetDefinition ()); var curMember = parsedFile.GetMember (loc); if (curMember != null) rctx = rctx.WithCurrentMember (curMember.CreateResolved (rctx)); } engine.ctx = rctx; engine.CSharpParsedFile = parsedFile; engine.ProjectContent = pctx; engine.Unit = compilationUnit; return engine.GetParameterDataProvider (cursorPosition, doc.GetCharAt (cursorPosition - 1)); }
protected override CodeCompletionResult GetCodeCompletionResult(bool controlSpace) { var document = codeEditor.TextEditor.Document; int offset = codeEditor.TextEditor.CaretOffset; var result = new CodeCompletionResult(); try { var completionContext = new CSharpCodeCompletionContext(document, offset, projectContent); var completionFactory = new CSharpCodeCompletionDataFactory(completionContext); var cce = new CSharpCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); char completionChar = completionContext.Document.GetCharAt(completionContext.Offset - 1); int startPos, triggerWordLength; IEnumerable<ICompletionData> completionData; if (controlSpace) { if (!cce.TryGetCompletionWord(completionContext.Offset, out startPos, out triggerWordLength)) { startPos = completionContext.Offset; triggerWordLength = 0; } completionData = cce.GetCompletionData(startPos, true); } else { startPos = completionContext.Offset; if (char.IsLetterOrDigit(completionChar) || completionChar == '_') { if (startPos > 1 && char.IsLetterOrDigit(completionContext.Document.GetCharAt((startPos - 2)))) return result; completionData = cce.GetCompletionData(startPos, false); triggerWordLength = 1; } else { completionData = cce.GetCompletionData(startPos, false); triggerWordLength = 0; } } result.TriggerWordLength = triggerWordLength; result.TriggerWord = completionContext.Document.GetText(completionContext.Offset - triggerWordLength, triggerWordLength); if (completionData.Any() && cce.AutoCompleteEmptyMatch) { foreach (var completion in completionData) { var cast = completion as CompletionData; if (cast != null) { cast.TriggerWord = result.TriggerWord; cast.TriggerWordLength = result.TriggerWordLength; result.CompletionData.Add(cast); } } } if (!controlSpace) { var pce = new CSharpParameterCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(completionContext.Offset, completionChar); result.OverloadProvider = parameterDataProvider as IUpdatableOverloadProvider; } } catch { // ignore exceptions thrown during code completion } return result; }
bool ShowCompletion(ITextEditor editor, char completionChar, bool ctrlSpace) { CSharpCompletionContext completionContext; if (fileContent == null) { completionContext = CSharpCompletionContext.Get(editor); } else { completionContext = CSharpCompletionContext.Get(editor, context, currentLocation, fileContent); } if (completionContext == null) return false; int caretOffset; if (fileContent == null) { caretOffset = editor.Caret.Offset; currentLocation = editor.Caret.Location; } else { caretOffset = completionContext.Document.GetOffset(currentLocation); } var completionFactory = new CSharpCompletionDataFactory(completionContext, new CSharpResolver(completionContext.TypeResolveContextAtCaret)); CSharpCompletionEngine cce = new CSharpCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); var formattingOptions = CSharpFormattingPolicies.Instance.GetProjectOptions(completionContext.Compilation.GetProject()); cce.FormattingPolicy = formattingOptions.OptionsContainer.GetEffectiveOptions(); cce.EolMarker = DocumentUtilities.GetLineTerminator(completionContext.Document, currentLocation.Line); cce.IndentString = editor.Options.IndentationString; int startPos, triggerWordLength; IEnumerable<ICompletionData> completionData; if (ctrlSpace) { if (!cce.TryGetCompletionWord(caretOffset, out startPos, out triggerWordLength)) { startPos = caretOffset; triggerWordLength = 0; } completionData = cce.GetCompletionData(startPos, true); completionData = completionData.Concat(cce.GetImportCompletionData(startPos)); } else { startPos = caretOffset; if (char.IsLetterOrDigit (completionChar) || completionChar == '_') { if (!CodeCompletionOptions.CompleteWhenTyping) return false; if (startPos > 1 && char.IsLetterOrDigit (completionContext.Document.GetCharAt (startPos - 2))) return false; completionData = cce.GetCompletionData(startPos, false); startPos--; triggerWordLength = 1; } else { completionData = cce.GetCompletionData(startPos, false); triggerWordLength = 0; } } DefaultCompletionItemList list = new DefaultCompletionItemList(); list.Items.AddRange(FilterAndAddTemplates(editor, completionData.Cast<ICompletionItem>().ToList())); if (list.Items.Count > 0 && (ctrlSpace || cce.AutoCompleteEmptyMatch)) { list.SortItems(); list.PreselectionLength = caretOffset - startPos; list.PostselectionLength = Math.Max(0, startPos + triggerWordLength - caretOffset); list.SuggestedItem = list.Items.FirstOrDefault(i => i.Text == cce.DefaultCompletionString); editor.ShowCompletionWindow(list); return true; } if (CodeCompletionOptions.InsightEnabled && !ctrlSpace) { // Method Insight var pce = new CSharpParameterCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); var newInsight = pce.GetParameterDataProvider(caretOffset, completionChar) as CSharpMethodInsight; if (newInsight != null && newInsight.items.Count > 0) { newInsight.UpdateHighlightedParameter(pce); newInsight.Show(); return true; } } return false; }
bool ShowInsight( int caretOffset, CSharpCompletionContext completionContext, CSharpCompletionDataFactory completionFactory, char completionChar) { if (CodeCompletionOptions.InsightEnabled) { // Method Insight var pce = new CSharpParameterCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); var newInsight = pce.GetParameterDataProvider(caretOffset, completionChar) as CSharpMethodInsight; if (newInsight != null && newInsight.items.Count > 0) { newInsight.UpdateHighlightedParameter(pce); newInsight.Show(); return true; } } return false; }
public CodeCompletionResult GetCompletions(IDocument document, int offset, bool controlSpace, string usings, string variables = null) { var result = new CodeCompletionResult(); if (String.IsNullOrEmpty(document.FileName)) return result; var completionContext = new CSharpCompletionContext(document, offset, projectContent, usings, variables); var completionFactory = new CSharpCompletionDataFactory(completionContext.TypeResolveContextAtCaret, completionContext); var cce = new CSharpCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); cce.EolMarker = Environment.NewLine; cce.FormattingPolicy = FormattingOptionsFactory.CreateSharpDevelop(); var completionChar = completionContext.Document.GetCharAt(completionContext.Offset - 1); int startPos, triggerWordLength; IEnumerable<ICSharpCode.NRefactory.Completion.ICompletionData> completionData; if (controlSpace) { if (!cce.TryGetCompletionWord(completionContext.Offset, out startPos, out triggerWordLength)) { startPos = completionContext.Offset; triggerWordLength = 0; } completionData = cce.GetCompletionData(startPos, true); //this outputs tons of available entities //if (triggerWordLength == 0) // completionData = completionData.Concat(cce.GetImportCompletionData(startPos)); } else { startPos = completionContext.Offset; if (char.IsLetterOrDigit(completionChar) || completionChar == '_') { if (startPos > 1 && char.IsLetterOrDigit(completionContext.Document.GetCharAt(startPos - 2))) return result; completionData = cce.GetCompletionData(startPos, false); startPos--; triggerWordLength = 1; } else { completionData = cce.GetCompletionData(startPos, false); triggerWordLength = 0; } } result.TriggerWordLength = triggerWordLength; result.TriggerWord = completionContext.Document.GetText(completionContext.Offset - triggerWordLength, triggerWordLength); Debug.Print("Trigger word: '{0}'", result.TriggerWord); //cast to AvalonEdit completion data and add to results foreach (var completion in completionData) { var cshellCompletionData = completion as ICSharpCode.CodeCompletion.DataItems.CompletionData; if (cshellCompletionData != null) { cshellCompletionData.TriggerWord = result.TriggerWord; cshellCompletionData.TriggerWordLength = result.TriggerWordLength; if (completion is ICSharpCode.CodeCompletion.DataItems.EntityCompletionData) { string typename = String.Empty; var entity = cshellCompletionData as ICSharpCode.CodeCompletion.DataItems.EntityCompletionData; if (entity.Entity is ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultResolvedTypeDefinition) { var def = entity.Entity as ICSharpCode.NRefactory.TypeSystem.Implementation.DefaultResolvedTypeDefinition; typename = def.FullTypeName.ReflectionName; } else { typename = entity.Entity.DeclaringType.FullName; } Type type = Type.GetType(typename); if (!AllowedType(type)) { continue; } } result.CompletionData.Add(cshellCompletionData); // piggyback 'region' preprocessor to add our own if (cshellCompletionData.DisplayText == "region") { DataItems.CompletionData import = new DataItems.CompletionData("import"); import.CompletionCategory = cshellCompletionData.CompletionCategory; import.Image = cshellCompletionData.Image; result.CompletionData.Add(import); } } } //method completions if (!controlSpace) { // Method Insight var pce = new CSharpParameterCompletionEngine( completionContext.Document, completionContext.CompletionContextProvider, completionFactory, completionContext.ProjectContent, completionContext.TypeResolveContextAtCaret ); var parameterDataProvider = pce.GetParameterDataProvider(completionContext.Offset, completionChar); result.OverloadProvider = parameterDataProvider as IOverloadProvider; } return result; }