Ejemplo n.º 1
0
		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);
			}
		}
Ejemplo n.º 2
0
		void OnToolTipRequest(object sender, TextEditor.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);
				}
				
				TextEditor.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);
				}
			}
		}
		public void Init()
		{
			HostCallback.GetCurrentProjectContent = delegate {
				return ParserService.CurrentProjectContent;
			};
			
			ef = new CSharpExpressionFinder(null);
		}
Ejemplo n.º 4
0
		public void Init()
		{
			HostCallback.GetParseInformation = ParserService.GetParseInformation;
			HostCallback.GetCurrentProjectContent = delegate {
				return ParserService.CurrentProjectContent;
			};
			
			ef = new CSharpExpressionFinder("test.cs");
		}
		protected override void MarkWords(int lineNumber, LineSegment currentLine, List<TextWord> words)
		{
			if (IsInMultilineCommentOrStringLiteral(currentLine)) {
				return;
			}
			ParseInformation parseInfo = ParserService.GetParseInformation(this.TextEditor.FileName);
			if (parseInfo == null) return;
			
			CSharpExpressionFinder finder = new CSharpExpressionFinder(parseInfo);
			Func<string, int, ExpressionResult> findExpressionMethod;
			IClass callingClass = parseInfo.MostRecentCompilationUnit.GetInnermostClass(lineNumber, 0);
			if (callingClass != null) {
				if (GetCurrentMember(callingClass, lineNumber, 0) != null) {
					findExpressionMethod = finder.FindFullExpressionInMethod;
				} else {
					findExpressionMethod = finder.FindFullExpressionInTypeDeclaration;
				}
			} else {
				findExpressionMethod = finder.FindFullExpression;
			}
			
			string lineText = this.Document.GetText(currentLine.Offset, currentLine.Length);
			bool changedLine = false;
			// now go through the word list:
			foreach (TextWord word in words) {
				if (word.IsWhiteSpace) continue;
				if (char.IsLetter(lineText[word.Offset]) || lineText[word.Offset] == '_') {
					ExpressionResult result = findExpressionMethod(lineText, word.Offset);
					if (result.Expression != null) {
						// result.Expression
						if (ICSharpCode.NRefactory.Parser.CSharp.Keywords.IsNonIdentifierKeyword(result.Expression))
							continue;
						// convert text editor to DOM coordinates:
						resolveCount++;
						ResolveResult rr = ParserService.Resolve(result, lineNumber + 1, word.Offset + 1, this.TextEditor.FileName, this.TextEditor.Text);
						if (rr is MixedResolveResult || rr is TypeResolveResult) {
							changedLine = true;
							word.SyntaxColor = this.Document.HighlightingStrategy.GetColorFor("TypeReference");
						} else if (rr == null) {
							changedLine = true;
							word.SyntaxColor = this.Document.HighlightingStrategy.GetColorFor("UnknownEntity");
						}
					}
				}
			}
			
			if (markingOutstanding && changedLine) {
				this.Document.RequestUpdate(new TextAreaUpdate(TextAreaUpdateType.SingleLine, lineNumber));
			}
		}
		public override bool HandleKeyPress(SharpDevelopTextAreaControl editor, char ch)
		{
			CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.FileName);
			int cursor = editor.ActiveTextAreaControl.Caret.Offset;
			ExpressionContext context = null;
			if (ch == '(') {
				if (CodeCompletionOptions.KeywordCompletionEnabled) {
					switch (editor.GetWordBeforeCaret().Trim()) {
						case "for":
						case "lock":
							context = ExpressionContext.Default;
							break;
						case "using":
							context = ExpressionContext.TypeDerivingFrom(ParserService.CurrentProjectContent.GetClass("System.IDisposable"), false);
							break;
						case "catch":
							context = ExpressionContext.TypeDerivingFrom(ParserService.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.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"))) {
								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 == ';') {
				LineSegment curLine = editor.Document.GetLineSegmentForOffset(cursor);
				// don't return true when inference succeeds, otherwise the ';' won't be added to the document.
				TryDeclarationTypeInference(editor, curLine);
			}
			
			return base.HandleKeyPress(editor, ch);
		}
		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;
		}
		bool IsInComment(SharpDevelopTextAreaControl editor)
		{
			CSharpExpressionFinder ef = new CSharpExpressionFinder(editor.FileName);
			int cursor = editor.ActiveTextAreaControl.Caret.Offset - 1;
			return ef.FilterComments(editor.Document.GetText(0, cursor + 1), ref cursor) == null;
		}
        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 ExpressionResult getExpressionFromTextArea(TextArea textArea)
 {
     IExpressionFinder finder = new CSharpExpressionFinder(parseInformation);
     return finder.FindExpression(textArea.Document.TextContent, textArea.Caret.Offset);            
 }
        private ResolveResult resolveLocation(TextLocation logicalPosition)
        {
           var textArea = TextEditor.ActiveTextAreaControl.TextArea;
            string targetText;
            int targetOffset;
            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;
            }

            //getExpressionFromTextArea(textArea);
            var expressionFinder = new CSharpExpressionFinder(parseInformation);
            var 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);
            }
            
            var resolver = new NRefactoryResolver(this.myProjectContent.Language);
            var rr = resolver.Resolve(expression, parseInformation, targetText);                                                

            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;
 }
		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;
		}
        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;
        }
		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)"));
		}