protected ExpressionResult GetExpression(EditViewControl textArea) { SyntaxDocument document = textArea.Document; IExpressionFinder expressionFinder = null; if (Parser.ProjectParser.CurrentProjectContent.Language == LanguageProperties.CSharp) { expressionFinder = new CSharpExpressionFinder(this.fileName); } else { expressionFinder = new VBExpressionFinder(); } if (expressionFinder == null) { return(new ExpressionResult(textArea.Caret.CurrentWord.Text)); } else { TextRange range = new TextRange(0, 0, textArea.Caret.Position.X, textArea.Caret.Position.Y); ExpressionResult res = expressionFinder.FindExpression(document.GetRange(range), textArea.Caret.Offset - 1); if (overrideContext != null) { res.Context = overrideContext; } return(res); } }
private void OnToolTipRequest(object sender, ToolTipRequestEventArgs e) { if (e.InDocument && !e.ToolTipShown) { IExpressionFinder expressionFinder; if (MainForm.IsVisualBasic) { expressionFinder = new VBExpressionFinder(); } else { expressionFinder = new CSharpExpressionFinder(mainForm.parseInformation); } ExpressionResult expression = expressionFinder.FindFullExpression( editor.Text, editor.Document.PositionToOffset(e.LogicalPosition)); if (expression.Region.IsEmpty) { expression.Region = new DomRegion(e.LogicalPosition.Line + 1, e.LogicalPosition.Column + 1); } TextArea textArea = editor.ActiveTextAreaControl.TextArea; NRefactoryResolver resolver = new NRefactoryResolver(mainForm.myProjectContent.Language); ResolveResult rr = resolver.Resolve(expression, mainForm.parseInformation, textArea.MotherTextEditorControl.Text); string toolTipText = GetText(rr); if (toolTipText != null) { e.ShowToolTip(toolTipText); } } }
bool IsInComment(ITextEditor editor) { CSharpExpressionFinder ef = CreateExpressionFinder(editor.Document.Text); int cursor = editor.Caret.Offset - 1; return(ef.FilterComments(editor.Document.GetText(0, cursor + 1), ref cursor) == null); }
bool ShowNewCompletion(ITextEditor editor) { CSharpExpressionFinder ef = CreateExpressionFinder(editor.Document.Text); int cursor = editor.Caret.Offset; string documentToCursor = editor.Document.GetText(0, cursor); ExpressionResult expressionResult = ef.FindExpression(documentToCursor, cursor); if (expressionResult.Context.IsObjectCreation) { var currentLine = editor.Document.GetLineForOffset(cursor); string lineText = editor.Document.GetText(currentLine.Offset, cursor - currentLine.Offset); // when the new follows an assignment, improve code-completion by detecting the // type of the variable that is assigned to if (lineText.Replace(" ", "").EndsWith("=new")) { int pos = lineText.LastIndexOf('='); ExpressionContext context = FindExactContextForNewCompletion(editor, documentToCursor, currentLine, pos); if (context != null) { expressionResult.Context = context; } } ShowCompletion(new NRefactoryCtrlSpaceCompletionItemProvider(LanguageProperties.CSharp, expressionResult.Context, ProjectContent), editor, ProjectContent); return(true); } return(false); }
private bool IsInComment(CodeEditorControl editor) { CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.ActiveViewControl.FileName); int cursor = editor.ActiveViewControl.Caret.Offset - 1; return(ef.FilterComments(ScriptControl.Parser.ProjectParser.GetFileContents(editor.FileName).Substring(0, cursor + 1), ref cursor) == null); }
private object GetParameter() { var expressionFinder = new CSharpExpressionFinder(ParserService.GetParseInformation(AssociatedObject.Text, ProjectContent)); var caret = AssociatedObject.TextArea.Caret; var result = expressionFinder.FindFullExpression(AssociatedObject.Text, caret.Offset); var resolveResult = ParserService.Resolve(result, caret.Line, caret.Column, AssociatedObject.Text, ProjectContent); if (resolveResult == null || !resolveResult.IsValid) { return(null); } if (resolveResult is MemberResolveResult) { return(((MemberResolveResult)resolveResult).ResolvedMember); } if (resolveResult is TypeResolveResult) { return(((TypeResolveResult)resolveResult).ResolvedClass); } if (resolveResult is NamespaceResolveResult) { return(new NamespaceEntry(((NamespaceResolveResult)resolveResult).Name)); } return(null); }
bool IsInComment(SharpDevelopTextAreaControl editor) { CSharpExpressionFinder ef = CreateExpressionFinder(editor.FileName); int cursor = editor.ActiveTextAreaControl.Caret.Offset - 1; return(ef.FilterComments(editor.Document.GetText(0, cursor + 1), ref cursor) == null); }
bool ShowNewCompletion(SharpDevelopTextAreaControl editor) { CSharpExpressionFinder ef = CreateExpressionFinder(editor.FileName); int cursor = editor.ActiveTextAreaControl.Caret.Offset; string documentToCursor = editor.Document.GetText(0, cursor); ExpressionResult expressionResult = ef.FindExpression(documentToCursor, cursor); LoggingService.Debug("ShowNewCompletion: expression is " + expressionResult); if (expressionResult.Context.IsObjectCreation) { LineSegment currentLine = editor.Document.GetLineSegmentForOffset(cursor); string lineText = editor.Document.GetText(currentLine.Offset, cursor - currentLine.Offset); // when the new follows an assignment, improve code-completion by detecting the // type of the variable that is assigned to if (lineText.Replace(" ", "").EndsWith("=new")) { int pos = lineText.LastIndexOf('='); ExpressionContext context = FindExactContextForNewCompletion(editor, documentToCursor, currentLine, pos); if (context != null) { expressionResult.Context = context; } } editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(expressionResult.Context), ' '); return(true); } return(false); }
public void Init() { HostCallback.GetCurrentProjectContent = delegate { return(ParserService.CurrentProjectContent); }; ef = new CSharpExpressionFinder(null); }
public void Init() { HostCallback.GetParseInformation = ParserService.GetParseInformation; HostCallback.GetCurrentProjectContent = delegate { return(ParserService.CurrentProjectContent); }; ef = new CSharpExpressionFinder("test.cs"); }
public void RemoveLastPart() { CSharpExpressionFinder f = new CSharpExpressionFinder(null); Assert.AreEqual("arr", f.RemoveLastPart("arr[i]")); Assert.AreEqual("obj", f.RemoveLastPart("obj.Field")); Assert.AreEqual("obj.Method", f.RemoveLastPart("obj.Method(args, ...)")); Assert.AreEqual("obj.Method", f.RemoveLastPart("obj.Method(complex[1].SubExpression)")); }
public CSDocument(string name, Uri location) : base(name, location) { FSyncContext = SynchronizationContext.Current; Debug.Assert(FSyncContext != null, "Current SynchronizationContext must be set."); FParseInfo = new ParseInformation(); ExpressionFinder = new CSharpExpressionFinder(FParseInfo); }
public ExpressionResult getExpressionFromCodeSnippet(TextArea textArea) { IExpressionFinder finder = new CSharpExpressionFinder(this.parseInformation); var firstMethodOffset = calculateFirstMethodOffset(); var adjustedSnippeetText = getAdjustedSnippetText(textArea, firstMethodOffset); var offset = firstMethodOffset + textArea.Caret.Offset; var expression = finder.FindExpression(adjustedSnippeetText, offset); return(expression); }
public void SetupDataProvider(string fileName) { IExpressionFinder expressionFinder; if (IntellisenseForm.SupportedLanguage == ESupportedLanguage.VisualBasic) { expressionFinder = new VBExpressionFinder(); } else { expressionFinder = new CSharpExpressionFinder(iForm.ParseInformation); } //TextLocation position = m_textarea.Caret.Position; //ExpressionResult expression = expressionFinder.FindFullExpression(m_textarea.MotherTextEditorControl.Text, m_textarea.Document.PositionToOffset(position)); //if (expression.Region.IsEmpty) //{ // expression.Region = new DomRegion(position.Line + 1, position.Column + 1); //} ExpressionResult expression = expressionFinder.FindFullExpression( m_textarea.MotherTextEditorControl.Text, m_textarea.MotherTextEditorControl.Document.PositionToOffset(m_textarea.Caret.Position) - 1); if (expression.Region.IsEmpty) { expression.Region = new DomRegion(m_textarea.Caret.Position.Line + 1, m_textarea.Caret.Position.Column + 1); } NRefactoryResolver resolver = new NRefactoryResolver(iForm.ProjectContent.Language); ResolveResult rr = resolver.Resolve(expression, iForm.ParseInformation, m_textarea.MotherTextEditorControl.Text); List <string> lines = new List <string>(); if (rr is MethodGroupResolveResult) { MethodGroupResolveResult mrr = rr as MethodGroupResolveResult; IAmbience ambience = IntellisenseForm.SupportedLanguage == ESupportedLanguage.VisualBasic ? (IAmbience) new VBNetAmbience() : new CSharpAmbience(); ambience.ConversionFlags = ConversionFlags.StandardConversionFlags | ConversionFlags.ShowAccessibility; foreach (MethodGroup methodgroup in mrr.Methods) { foreach (IMethod method in methodgroup) { lines.Add(ToolTipProvider.GetMemberText(ambience, method)); } } } m_insighttext = (lines.Count > 0) ? lines.ToArray() : null; m_argumentstartoffset = m_textarea.Caret.Offset; }
ExpressionContext FindExactContextForNewCompletion(SharpDevelopTextAreaControl editor, string documentToCursor, LineSegment currentLine, int pos) { CSharpExpressionFinder ef = CreateExpressionFinder(editor.FileName); // find expression on left hand side of the assignment ExpressionResult lhsExpr = ef.FindExpression(documentToCursor, currentLine.Offset + pos); if (lhsExpr.Expression != null) { ResolveResult rr = ParserService.Resolve(lhsExpr, currentLine.LineNumber, pos, editor.FileName, editor.Text); if (rr != null && rr.ResolvedType != null) { ExpressionContext context; IClass c; if (rr.ResolvedType.IsArrayReturnType) { // when creating an array, all classes deriving from the array's element type are allowed IReturnType elementType = rr.ResolvedType.CastToArrayReturnType().ArrayElementType; c = elementType != null?elementType.GetUnderlyingClass() : null; context = ExpressionContext.TypeDerivingFrom(elementType, false); } else { // when creating a normal instance, all non-abstract classes deriving from the type // are allowed c = rr.ResolvedType.GetUnderlyingClass(); context = ExpressionContext.TypeDerivingFrom(rr.ResolvedType, true); } if (c != null && context.ShowEntry(c)) { // Try to suggest an entry (List<int> a = new => suggest List<int>). string suggestedClassName = LanguageProperties.CSharp.CodeGenerator.GenerateCode( CodeGenerator.ConvertType( rr.ResolvedType, new ClassFinder(ParserService.GetParseInformation(editor.FileName), editor.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1) ), ""); if (suggestedClassName != c.Name) { // create an IClass instance that includes the type arguments in its name context.SuggestedItem = new RenamedClass(c, suggestedClassName); } else { context.SuggestedItem = c; } } return(context); } } return(null); }
private bool ShowNewCompletion(CodeEditorControl editor) { CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.ActiveViewControl.FileName); int cursor = editor.ActiveViewControl.Caret.Offset; ExpressionContext context = ef.FindExpression(ScriptControl.Parser.ProjectParser.GetFileContents(editor.FileName).Substring(0, cursor) + " T.", cursor + 2).Context; if (context.IsObjectCreation) { editor.ActiveViewControl.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ' '); return(true); } return(false); }
bool ShowNewCompletion(SharpDevelopTextAreaControl editor) { CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.FileName); int cursor = editor.ActiveTextAreaControl.Caret.Offset; ExpressionContext context = ef.FindExpression(editor.Document.GetText(0, cursor) + " T.", cursor + 2).Context; if (context.IsObjectCreation) { editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ' '); return(true); } return(false); }
public void SetupDataProvider(string fileName, EditViewControl textArea) { if (setupOnlyOnce && this.textArea != null) { return; } SyntaxDocument document = textArea.Document; this.fileName = fileName; this.document = document; this.textArea = textArea; int useOffset = (lookupOffset < 0) ? textArea.Caret.Offset : lookupOffset; initialOffset = useOffset; IExpressionFinder expressionFinder = null; if (Parser.ProjectParser.CurrentProjectContent.Language == LanguageProperties.CSharp) { expressionFinder = new CSharpExpressionFinder(fileName); } else { expressionFinder = new VBExpressionFinder(); } ExpressionResult expressionResult; if (expressionFinder == null) { expressionResult = new ExpressionResult(textArea.Caret.CurrentWord.Text); //TextUtilities.GetExpressionBeforeOffset(textArea, useOffset)); } else { expressionResult = expressionFinder.FindExpression(Parser.ProjectParser.GetFileContents(fileName), useOffset - 1); } if (expressionResult.Expression == null) // expression is null when cursor is in string/comment { return; } expressionResult.Expression = expressionResult.Expression.Trim(); // the parser works with 1 based coordinates TextPoint tp = document.IntPosToPoint(useOffset + 1); int caretLineNumber = tp.Y; int caretColumn = tp.X; SetupDataProvider(fileName, document, expressionResult, caretLineNumber, caretColumn); }
public void SetupDataProvider(string fileName) { IExpressionFinder expressionFinder; if (IntellisenseForm.SupportedLanguage == SupportedLanguage.VisualBasic) { expressionFinder = new VBExpressionFinder(); } else { expressionFinder = new CSharpExpressionFinder(_iForm.ParseInformation); } //TextLocation position = _textArea.Caret.Position; //ExpressionResult expression = expressionFinder.FindFullExpression(_textArea.MotherTextEditorControl.Text, _textArea.Document.PositionToOffset(position)); //if (expression.Region.IsEmpty) //{ // expression.Region = new DomRegion(position.Line + 1, position.Column + 1); //} var expression = expressionFinder.FindFullExpression( _textArea.MotherTextEditorControl.Text, _textArea.MotherTextEditorControl.Document.PositionToOffset(_textArea.Caret.Position) - 1); if (expression.Region.IsEmpty) { expression.Region = new DomRegion(_textArea.Caret.Position.Line + 1, _textArea.Caret.Position.Column + 1); } var resolver = new NRefactoryResolver(_iForm.ProjectContent.Language); var rr = resolver.Resolve(expression, _iForm.ParseInformation, _textArea.MotherTextEditorControl.Text); var lines = new List <string>(); if (rr is MethodGroupResolveResult) { var mrr = rr as MethodGroupResolveResult; var ambience = IntellisenseForm.SupportedLanguage == SupportedLanguage.VisualBasic ? (IAmbience) new VBNetAmbience() : new CSharpAmbience(); ambience.ConversionFlags = ConversionFlags.StandardConversionFlags | ConversionFlags.ShowAccessibility; lines.AddRange(mrr.Methods.SelectMany(methodgroup => methodgroup, (methodgroup, method) => ToolTipProvider.GetMemberText(ambience, method))); } _insighText = lines.Count > 0 ? lines.ToArray() : null; ArgumentStartOffset = _textArea.Caret.Offset; }
protected override bool IsInComment(IDocument document, int offset) { var csDocument = FEditor.TextDocument as CSDocument; var expressionFinder = new CSharpExpressionFinder(csDocument.ParseInfo); int length = offset + 1; if (length <= document.TextLength) { return(expressionFinder.FilterComments(document.GetText(0, length), ref offset) == null); } else { return(false); } }
private ResolveResult resolveLocation(TextLocation logicalPosition) { var textArea = textEditor.ActiveTextAreaControl.TextArea; var targetText = ""; var targetOffset = 0; if (CodeCompleteCaretLocationOffset.Line == 0) { targetText = textEditor.get_Text();//.Text; targetOffset = textEditor.Document.PositionToOffset(logicalPosition); } else { var firstMethodOffset = calculateFirstMethodOffset(); targetText = getAdjustedSnippetText(textArea, firstMethodOffset); targetOffset = firstMethodOffset + textArea.Caret.Offset; } ExpressionResult expression; //getExpressionFromTextArea(textArea); IExpressionFinder expressionFinder; expressionFinder = new CSharpExpressionFinder(this.parseInformation); expression = expressionFinder.FindFullExpression(targetText, targetOffset); /*IExpressionFinder expressionFinder; * expressionFinder = new CSharpExpressionFinder(this.parseInformation); * ExpressionResult expression = expressionFinder.FindFullExpression( * textEditor.Text, * textEditor.Document.PositionToOffset(logicalPosition)); */ if (expression.Region.IsEmpty) { expression.Region = new DomRegion(logicalPosition.Line + 1, logicalPosition.Column + 1); } NRefactoryResolver resolver = new NRefactoryResolver(this.myProjectContent.Language); ResolveResult rr = resolver.Resolve(expression, this.parseInformation, targetText); //textArea.MotherTextEditorControl.Text); return(rr); }
/// <summary> /// Find the expression the cursor is at. /// Also determines the context (using statement, "new"-expression etc.) the /// cursor is at. /// </summary> private ExpressionResult FindExpression(TextArea textArea) { IExpressionFinder finder; if (MainForm.IsVisualBasic) { finder = new VBExpressionFinder(); } else { finder = new CSharpExpressionFinder(mainForm.parseInformation); } ExpressionResult expression = finder.FindExpression(textArea.Document.TextContent, textArea.Caret.Offset); if (expression.Region.IsEmpty) { expression.Region = new DomRegion(textArea.Caret.Line + 1, textArea.Caret.Column + 1); } return(expression); }
private void OnToolTipRequest(object sender, TextEditor.ToolTipRequestEventArgs e) { if (!e.InDocument || e.ToolTipShown) { return; } IExpressionFinder expressionFinder; if (IntellisenseForm.SupportedLanguage == SupportedLanguage.VisualBasic) { expressionFinder = new VBExpressionFinder(); } else { expressionFinder = new CSharpExpressionFinder(_iForm.ParseInformation); } var expression = expressionFinder.FindFullExpression( _editor.Text, _editor.Document.PositionToOffset(e.LogicalPosition)); if (expression.Region.IsEmpty) { expression.Region = new DomRegion(e.LogicalPosition.Line + 1, e.LogicalPosition.Column + 1); } var textArea = _editor.ActiveTextAreaControl.TextArea; var resolver = new NRefactoryResolver(_iForm.ProjectContent.Language); var rr = resolver.Resolve(expression, _iForm.ParseInformation, textArea.MotherTextEditorControl.Text); var toolTipText = GetText(rr); if (toolTipText != null) { e.ShowToolTip(toolTipText); } }
public CSDocument(string name, Uri location) : base(name, location) { FParseInfo = new ParseInformation(); ExpressionFinder = new CSharpExpressionFinder(FParseInfo); }
public override bool HandleKeyPress(CodeEditorControl editor, char ch) { CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.ActiveViewControl.FileName); int cursor = editor.ActiveViewControl.Caret.Offset; ExpressionContext context = null; if (ch == '(') { if (CodeCompletionOptions.KeywordCompletionEnabled) { switch (editor.ActiveViewControl.Caret.CurrentWord.Text.Trim()) { case "for": case "lock": context = ExpressionContext.Default; break; case "using": context = ExpressionContext.TypeDerivingFrom(ScriptControl.Parser.ProjectParser.CurrentProjectContent.GetClass("System.IDisposable"), false); break; case "catch": context = ExpressionContext.TypeDerivingFrom(ScriptControl.Parser.ProjectParser.CurrentProjectContent.GetClass("System.Exception"), false); break; case "foreach": case "typeof": case "sizeof": case "default": context = ExpressionContext.Type; break; } } if (context != null) { if (IsInComment(editor)) { return(false); } editor.ActiveViewControl.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ch); return(true); } else if (EnableMethodInsight) { editor.ActiveViewControl.ShowInsightWindow(new MethodInsightDataProvider()); return(true); } return(false); } else if (ch == '[') { Word curWord = editor.ActiveViewControl.Caret.CurrentWord; //Word preWord = curWord.Row.FormattedWords[curWord.Index-1]; //int lineOffset = editor.ActiveViewControl.Document.Text.Substring(0, editor.ActiveViewControl.Caret.Offset - editor.ActiveViewControl.Caret.Position.X).Length; //TextPoint tp = editor.ActiveViewControl.Document.IntPosToPoint(lineOffset); if (curWord.Text.Length == 0) { // [ is first character on the line // -> Attribute completion editor.ActiveViewControl.ShowCompletionWindow(new AttributesDataProvider(ScriptControl.Parser.ProjectParser.CurrentProjectContent), ch); return(true); } } else if (ch == ',' && CodeCompletionOptions.InsightRefreshOnComma && CodeCompletionOptions.InsightEnabled) { if (InsightRefreshOnComma(editor, ch)) { return(true); } } else if (ch == '=') { string curLine = editor.ActiveViewControl.Caret.CurrentRow.Text; string documentText = ScriptControl.Parser.ProjectParser.GetFileContents(editor.FileName); int position = editor.ActiveViewControl.Caret.Offset - 2; if (position > 0 && (documentText[position + 1] == '+')) { ExpressionResult result = ef.FindFullExpression(documentText, position); if (result.Expression != null) { ResolveResult resolveResult = ScriptControl.Parser.ProjectParser.GetResolver().Resolve(result, editor.ActiveViewControl.Caret.Position.Y + 1, editor.ActiveViewControl.Caret.Position.X + 1, editor.ActiveViewControl.FileName, documentText); if (resolveResult != null && resolveResult.ResolvedType != null) { IClass underlyingClass = resolveResult.ResolvedType.GetUnderlyingClass(); if (underlyingClass == null && resolveResult.ResolvedType.FullyQualifiedName.Length > 0) { underlyingClass = ScriptControl.Parser.ProjectParser.CurrentProjectContent.GetClass(resolveResult.ResolvedType.FullyQualifiedName); } //if (underlyingClass != null && underlyingClass.IsTypeInInheritanceTree(ScriptControl.Parser.ProjectParser.CurrentProjectContent.GetClass("System.MulticastDelegate"))) { if (underlyingClass != null && underlyingClass.IsTypeInInheritanceTree(ScriptControl.Parser.ProjectParser.CurrentProjectContent.GetClass("System.Delegate"))) { //EventHandlerCompletitionDataProvider eventHandlerProvider = new EventHandlerCompletitionDataProvider(result.Expression, resolveResult); //eventHandlerProvider.InsertSpace = true; //editor.ActiveViewControl.ShowCompletionWindow(eventHandlerProvider, ch); } } } } else if (position > 0) { ExpressionResult result = ef.FindFullExpression(documentText, position); if (result.Expression != null) { ResolveResult resolveResult = ScriptControl.Parser.ProjectParser.GetResolver().Resolve(result, editor.ActiveViewControl.Caret.Position.Y + 1, editor.ActiveViewControl.Caret.Position.X + 1, editor.ActiveViewControl.FileName, documentText); if (resolveResult != null && resolveResult.ResolvedType != null) { if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch)) { return(true); } } } } } else if (ch == ';') { string curLine = editor.ActiveViewControl.Caret.CurrentRow.Text; // don't return true when inference succeeds, otherwise the ';' won't be added to the document. TryDeclarationTypeInference(editor, curLine); } return(base.HandleKeyPress(editor, ch)); }
public override CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch, IProjectContent projectContent) { CSharpExpressionFinder ef = CreateExpressionFinder(editor.Document.Text); int cursor = editor.Caret.Offset; CodeCompletionKeyPressResult?returnResult = null; switch (ch) { case '[': { var line = editor.Document.GetLineForOffset(cursor); /* TODO: AVALONEDIT Reimplement this * if (TextUtilities.FindPrevWordStart(editor.ActiveTextAreaControl.Document, cursor) <= line.Offset) { * // [ is first character on the line * // -> Attribute completion * editor.ShowCompletionWindow(new AttributesDataProvider(ParserService.CurrentProjectContent), ch); * return true; * }*/ } break; case ' ': returnResult = CompleteSpace(editor, ef); break; case ',': returnResult = CompleteComma(editor, ch, projectContent); break; case '=': returnResult = CompleteAssigment(editor, ch, ef); break; case '.': returnResult = ShowDotCompletion(editor); break; case '>': { if (IsInComment(editor)) { returnResult = CodeCompletionKeyPressResult.None; break; } returnResult = CompleteMore(editor, cursor, projectContent); } break; } if (returnResult.HasValue) { return(returnResult.Value); } if (char.IsLetter(ch) || ch == '_') { if (editor.SelectionLength > 0) { // allow code completion when overwriting an identifier int endOffset = editor.SelectionStart + editor.SelectionLength; // but block code completion when overwriting only part of an identifier if (endOffset < editor.Document.TextLength && char.IsLetterOrDigit(editor.Document.GetCharAt(endOffset))) { return(CodeCompletionKeyPressResult.None); } editor.Document.Remove(editor.SelectionStart, editor.SelectionLength); // Read caret position again after removal - this is required because the document might change in other // locations, too (e.g. bound elements in snippets). cursor = editor.Caret.Offset; } var prevChar = GetPreviousChar(editor, cursor); var afterUnderscore = prevChar == '_'; if (afterUnderscore) { cursor--; prevChar = GetPreviousChar(editor, cursor); } if (!char.IsLetterOrDigit(prevChar) && prevChar != '.' && !IsInComment(editor)) { var result = ef.FindExpression(editor.Document.Text, cursor); if (result.Context != ExpressionContext.IdentifierExpected) { var ctrlSpaceProvider = new NRefactoryCtrlSpaceCompletionItemProvider( LanguageProperties.CSharp, result.Context, ProjectContent) { ShowTemplates = true, AllowCompleteExistingExpression = afterUnderscore }; ShowCompletion(ctrlSpaceProvider, editor, projectContent); return(CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion); } } } return(base.HandleKeyPress(editor, ch, projectContent)); }
private CodeCompletionKeyPressResult?CompleteAssigment(ITextEditor editor, char ch, CSharpExpressionFinder ef) { CodeCompletionKeyPressResult?returnResult = null; string documentText = editor.Document.Text; int position = editor.Caret.Offset - 2; if (position > 0) { ExpressionResult result = ef.FindFullExpression(documentText, position); if (result.Expression != null) { ResolveResult resolveResult = ParserService.Resolve(result, editor.Caret.Line, editor.Caret.Column, editor.Document.Text, documentText, ProjectContent); if (resolveResult != null && resolveResult.ResolvedType != null) { if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch)) { returnResult = CodeCompletionKeyPressResult.Completed; } } } } return(returnResult); }
public void Init() { _projectContentRegistry = new ProjectContentRegistry(); ef = new CSharpExpressionFinder(null); }
public CSDocument(string name, string path) : base(name, path) { FParseInfo = new ParseInformation(); ExpressionFinder = new CSharpExpressionFinder(FParseInfo); }
public override bool HandleKeyPress(SharpDevelopTextAreaControl editor, char ch) { CSharpExpressionFinder ef = CreateExpressionFinder(editor.FileName); int cursor = editor.ActiveTextAreaControl.Caret.Offset; ExpressionContext context = null; if (ch == '(') { if (context != null) { if (IsInComment(editor)) { return(false); } editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(context), ch); return(true); } else if (EnableMethodInsight && CodeCompletionOptions.InsightEnabled) { editor.ShowInsightWindow(new MethodInsightDataProvider()); return(true); } return(false); } else if (ch == '[') { LineSegment line = editor.Document.GetLineSegmentForOffset(cursor); if (TextUtilities.FindPrevWordStart(editor.Document, cursor) <= line.Offset) { // [ is first character on the line // -> Attribute completion editor.ShowCompletionWindow(new AttributesDataProvider(ParserService.CurrentProjectContent), ch); return(true); } } else if (ch == ',' && CodeCompletionOptions.InsightRefreshOnComma && CodeCompletionOptions.InsightEnabled) { if (InsightRefreshOnComma(editor, ch)) { return(true); } } else if (ch == '=') { LineSegment curLine = editor.Document.GetLineSegmentForOffset(cursor); string documentText = editor.Text; int position = editor.ActiveTextAreaControl.Caret.Offset - 2; if (position > 0 && (documentText[position + 1] == '+')) { ExpressionResult result = ef.FindFullExpression(documentText, position); if (result.Expression != null) { ResolveResult resolveResult = ParserService.Resolve(result, editor.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, editor.FileName, documentText); if (resolveResult != null && resolveResult.ResolvedType != null) { IClass underlyingClass = resolveResult.ResolvedType.GetUnderlyingClass(); if (underlyingClass != null && underlyingClass.IsTypeInInheritanceTree(ParserService.CurrentProjectContent.GetClass("System.MulticastDelegate", 0))) { EventHandlerCompletitionDataProvider eventHandlerProvider = new EventHandlerCompletitionDataProvider(result.Expression, resolveResult); eventHandlerProvider.InsertSpace = true; editor.ShowCompletionWindow(eventHandlerProvider, ch); } } } } else if (position > 0) { ExpressionResult result = ef.FindFullExpression(documentText, position); if (result.Expression != null) { ResolveResult resolveResult = ParserService.Resolve(result, editor.ActiveTextAreaControl.Caret.Line + 1, editor.ActiveTextAreaControl.Caret.Column + 1, editor.FileName, documentText); if (resolveResult != null && resolveResult.ResolvedType != null) { if (ProvideContextCompletion(editor, resolveResult.ResolvedType, ch)) { return(true); } } } } } else if (ch == '.') { editor.ShowCompletionWindow(new CSharpCodeCompletionDataProvider(), ch); return(true); } else if (ch == '>') { if (IsInComment(editor)) { return(false); } char prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' '; if (prevChar == '-') { editor.ShowCompletionWindow(new PointerArrowCompletionDataProvider(), ch); return(true); } } if (char.IsLetter(ch) && CodeCompletionOptions.CompleteWhenTyping) { if (editor.ActiveTextAreaControl.SelectionManager.HasSomethingSelected) { // allow code completion when overwriting an identifier cursor = editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0].Offset; int endOffset = editor.ActiveTextAreaControl.SelectionManager.SelectionCollection[0].EndOffset; // but block code completion when overwriting only part of an identifier if (endOffset < editor.Document.TextLength && char.IsLetterOrDigit(editor.Document.GetCharAt(endOffset))) { return(false); } editor.ActiveTextAreaControl.SelectionManager.RemoveSelectedText(); editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursor); } char prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' '; bool afterUnderscore = prevChar == '_'; if (afterUnderscore) { cursor--; prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' '; } if (!char.IsLetterOrDigit(prevChar) && prevChar != '.' && !IsInComment(editor)) { ExpressionResult result = ef.FindExpression(editor.Text, cursor); LoggingService.Debug("CC: Beginning to type a word, result=" + result); if (result.Context != ExpressionContext.IdentifierExpected) { editor.ShowCompletionWindow(new CtrlSpaceCompletionDataProvider(result.Context) { ShowTemplates = true, AllowCompleteExistingExpression = afterUnderscore }, '\0'); } } } return(base.HandleKeyPress(editor, ch)); }