public virtual CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch, IProjectContent projectContent)
        {
            IInsightWindow insightWindow = null;

            switch (ch)
            {
            case '(':
                insightWindow = editor.ShowInsightWindow(new MethodInsightProvider(projectContent).ProvideInsight(editor));
                if (insightWindow != null && insightHandler != null)
                {
                    insightHandler.InitializeOpenedInsightWindow(editor, insightWindow);
                    insightHandler.HighlightParameter(insightWindow, 0);
                }
                return(CodeCompletionKeyPressResult.Completed);

                break;

            case '[':
                insightWindow = editor.ShowInsightWindow(new IndexerInsightProvider(projectContent).ProvideInsight(editor));
                if (insightWindow != null && insightHandler != null)
                {
                    insightHandler.InitializeOpenedInsightWindow(editor, insightWindow);
                }
                return(CodeCompletionKeyPressResult.Completed);

                break;

            case '<':
                ShowCompletion(new CommentCompletionItemProvider(), editor, projectContent);
                return(CodeCompletionKeyPressResult.Completed);

                break;

            case '.':
                ShowCompletion(new DotCodeCompletionItemProvider(projectContent), editor, projectContent);
                return(CodeCompletionKeyPressResult.Completed);

                break;

            case ' ':
                string word = editor.GetWordBeforeCaret();
                if (!String.IsNullOrEmpty(word))
                {
                    if (HandleKeyword(editor, word))
                    {
                        return(CodeCompletionKeyPressResult.Completed);
                    }
                }
                break;
            }
            return(CodeCompletionKeyPressResult.None);
        }
Beispiel #2
0
        public CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch)
        {
            if (LanguageUtils.IsInsideDocumentationComment(editor) && ch == '<')
            {
                new CommentCompletionItemProvider().ShowCompletion(editor);
                return(CodeCompletionKeyPressResult.Completed);
            }

            if (IsInComment(editor) || IsInString(editor))
            {
                return(CodeCompletionKeyPressResult.None);
            }

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

            VBNetExpressionFinder ef = new VBNetExpressionFinder(ParserService.GetParseInformation(editor.FileName));

            ExpressionResult result;

            switch (ch)
            {
            case '(':
                if (CodeCompletionOptions.InsightEnabled)
                {
                    IInsightWindow insightWindow = editor.ShowInsightWindow(new MethodInsightProvider().ProvideInsight(editor));
                    if (insightWindow != null)
                    {
                        insightHandler.InitializeOpenedInsightWindow(editor, insightWindow);
                        insightHandler.HighlightParameter(insightWindow, 0);
                    }
                    return(CodeCompletionKeyPressResult.Completed);
                }
                break;

            case ',':
                if (CodeCompletionOptions.InsightRefreshOnComma && CodeCompletionOptions.InsightEnabled)
                {
                    IInsightWindow insightWindow;
                    if (insightHandler.InsightRefreshOnComma(editor, ch, out insightWindow))
                    {
                        return(CodeCompletionKeyPressResult.Completed);
                    }
                }
                break;

            case '\n':
                TryDeclarationTypeInference(editor, editor.Document.GetLineForOffset(editor.Caret.Offset));
                break;

            case '.':
                string w = editor.GetWordBeforeCaret(); int index = w.IndexOf('.');

                if (index > -1 && w.Length - index == 2)
                {
                    index = editor.Caret.Offset - 2;
                }
                else
                {
                    index = editor.Caret.Offset;
                }

                result = ef.FindExpression(editor.Document.Text, index);
                LoggingService.Debug("CC: After dot, result=" + result + ", context=" + result.Context);
                if (ShowCompletion(result, editor, ch))
                {
                    return(CodeCompletionKeyPressResult.Completed);
                }
                else
                {
                    return(CodeCompletionKeyPressResult.None);
                }

            case '@':
                if (editor.Caret.Offset > 0 && editor.Document.GetCharAt(editor.Caret.Offset - 1) == '.')
                {
                    return(CodeCompletionKeyPressResult.None);
                }
                goto default;

            case ' ':
                editor.Document.Insert(editor.Caret.Offset, " ");
                result = ef.FindExpression(editor.Document.Text, editor.Caret.Offset);

                string word = editor.GetWordBeforeCaret().Trim();
                if (word.Equals("overrides", StringComparison.OrdinalIgnoreCase) || word.Equals("return", StringComparison.OrdinalIgnoreCase) || !LiteralMayFollow((BitArray)result.Tag) && !OperatorMayFollow((BitArray)result.Tag) && ExpressionContext.IdentifierExpected != result.Context)
                {
                    LoggingService.Debug("CC: After space, result=" + result + ", context=" + result.Context);
                    ShowCompletion(result, editor, ch);
                }
                return(CodeCompletionKeyPressResult.EatKey);

            default:
                if (CodeCompletionOptions.CompleteWhenTyping)
                {
                    int  cursor   = editor.Caret.Offset;
                    char prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' ';
                    char ppChar   = cursor > 2 ? editor.Document.GetCharAt(cursor - 2) : ' ';

                    result = ef.FindExpression(editor.Document.Text, cursor);

                    if ((result.Context != ExpressionContext.IdentifierExpected && char.IsLetter(ch)) &&
                        (!char.IsLetterOrDigit(prevChar) && prevChar != '.'))
                    {
                        if (prevChar == '@' && ppChar == '.')
                        {
                            return(CodeCompletionKeyPressResult.None);
                        }
                        if (IsTypeCharacter(ch, prevChar))
                        {
                            return(CodeCompletionKeyPressResult.None);
                        }
                        if (prevChar == '_')
                        {
                            result.Expression = '_' + result.Expression;
                            result.Region     = new DomRegion(result.Region.BeginLine, result.Region.BeginColumn - 1, result.Region.EndLine, result.Region.EndColumn);
                        }
                        LoggingService.Debug("CC: Beginning to type a word, result=" + result + ", context=" + result.Context);
                        ShowCompletion(result, editor, ch);
                        return(CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion);
                    }
                }
                break;
            }

            return(CodeCompletionKeyPressResult.None);
        }
		public virtual CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch)
		{
			switch (ch) {
				case '(':
					if (enableMethodInsight && CodeCompletionOptions.InsightEnabled) {
						IInsightWindow insightWindow = editor.ShowInsightWindow(new MethodInsightProvider().ProvideInsight(editor));
						if (insightWindow != null && insightHandler != null) {
							insightHandler.InitializeOpenedInsightWindow(editor, insightWindow);
							insightHandler.HighlightParameter(insightWindow, -1); // disable highlighting
						}
						return CodeCompletionKeyPressResult.Completed;
					}
					break;
				case '[':
					if (enableIndexerInsight && CodeCompletionOptions.InsightEnabled) {
						IInsightWindow insightWindow = editor.ShowInsightWindow(new IndexerInsightProvider().ProvideInsight(editor));
						if (insightWindow != null && insightHandler != null)
							insightHandler.InitializeOpenedInsightWindow(editor, insightWindow);
						return CodeCompletionKeyPressResult.Completed;
					}
					break;
				case '<':
					if (enableXmlCommentCompletion) {
						new CommentCompletionItemProvider().ShowCompletion(editor);
						return CodeCompletionKeyPressResult.Completed;
					}
					break;
				case '.':
					if (enableDotCompletion) {
						new DotCodeCompletionItemProvider().ShowCompletion(editor);
						return CodeCompletionKeyPressResult.Completed;
					}
					break;
				case ' ':
					if (CodeCompletionOptions.KeywordCompletionEnabled) {
						string word = editor.GetWordBeforeCaret();
						if (!string.IsNullOrEmpty(word)) {
							if (HandleKeyword(editor, word))
								return CodeCompletionKeyPressResult.Completed;
						}
					}
					break;
			}
			return CodeCompletionKeyPressResult.None;
		}
		public static VBNetCompletionItemList GenerateCompletionData(this ExpressionResult expressionResult, ITextEditor editor, char pressedKey)
		{
			VBNetCompletionItemList result = new VBNetCompletionItemList();
			
			IResolver resolver = ParserService.CreateResolver(editor.FileName);
			ParseInformation info = ParserService.GetParseInformation(editor.FileName);
			
			if (info == null)
				return result;
			
			List<ICompletionEntry> data = new List<ICompletionEntry>();
			
			bool completingDotExpression = false;
			IReturnType resolvedType = null;
			
			if (expressionResult.Context != ExpressionContext.Global && expressionResult.Context != ExpressionContext.TypeDeclaration) {
				if (expressionResult.Context == ExpressionContext.Importable
				    && string.IsNullOrWhiteSpace(expressionResult.Expression)) {
					expressionResult.Expression = "Global";
				} else if (pressedKey == '\0') {
					int idx = string.IsNullOrWhiteSpace(expressionResult.Expression)
						? -1
						: expressionResult.Expression.LastIndexOf('.');
					
					if (idx > -1) {
						expressionResult.Expression = expressionResult.Expression.Substring(0, idx);
						// its the same as if . was pressed
						completingDotExpression = true;
					} else {
						expressionResult.Expression = "";
					}
				}
				
				var rr = resolver.Resolve(expressionResult, info, editor.Document.Text);
				
				if (rr == null || !rr.IsValid || (pressedKey != '.' && !completingDotExpression)) {
					if (((BitArray)expressionResult.Tag)[Tokens.Identifier])
						data = new NRefactoryResolver(LanguageProperties.VBNet)
							.CtrlSpace(editor.Caret.Line, editor.Caret.Column, info, editor.Document.Text, expressionResult.Context,
							           ((NRefactoryCompletionItemList)result).ContainsItemsFromAllNamespaces);
				} else {
					if (rr is MethodGroupResolveResult) {
						IMethod singleMethod = ((MethodGroupResolveResult)rr).GetMethodWithEmptyParameterList();
						if (singleMethod != null)
							rr = new MemberResolveResult(rr.CallingClass, rr.CallingMember, singleMethod);
					}
					
					if (rr is IntegerLiteralResolveResult && pressedKey == '.')
						return result;
					
					data = rr.GetCompletionData(info.CompilationUnit.ProjectContent, ((NRefactoryCompletionItemList)result).ContainsItemsFromAllNamespaces) ?? new List<ICompletionEntry>();
					
					resolvedType = rr.ResolvedType;
				}
			}
			
			bool addedKeywords = false;
			
			if (expressionResult.Tag != null && (expressionResult.Context != ExpressionContext.Importable) && pressedKey != '.' && !completingDotExpression) {
				AddVBNetKeywords(data, (BitArray)expressionResult.Tag);
				addedKeywords = true;
			}
			
			CodeCompletionItemProvider.ConvertCompletionData(result, data, expressionResult.Context);
			
			if (addedKeywords && result.Items.Any())
				AddTemplates(editor, result);
			
			string word = editor.GetWordBeforeCaret().Trim();
			
			IClass c = GetCurrentClass(editor);
			IMember m = GetCurrentMember(editor);
			
			HandleKeyword(ref result,  resolvedType, word, c, m, editor, pressedKey);
			
			AddSpecialItems(ref result, info, resolvedType, m, expressionResult, editor);
			
			char prevChar;
			
			if (pressedKey == '\0') { // ctrl+space
				prevChar = editor.Caret.Offset > 0 ? editor.Document.GetCharAt(editor.Caret.Offset - 1) : '\0';
				word = char.IsLetterOrDigit(prevChar) || prevChar == '_' ? editor.GetWordBeforeCaret() : "";
				
				if (!string.IsNullOrWhiteSpace(word))
					result.PreselectionLength = word.Length;
			}
			
			prevChar =  editor.Caret.Offset > 0 ? editor.Document.GetCharAt(editor.Caret.Offset - 1) : '\0';
			
			if (prevChar == '_')
				result.PreselectionLength++;
			
			result.SortItems();
			
			return result;
		}
Beispiel #5
0
        public static VBNetCompletionItemList GenerateCompletionData(this ExpressionResult expressionResult, ITextEditor editor, char pressedKey)
        {
            VBNetCompletionItemList result = new VBNetCompletionItemList();

            IResolver        resolver = ParserService.CreateResolver(editor.FileName);
            ParseInformation info     = ParserService.GetParseInformation(editor.FileName);

            if (info == null)
            {
                return(result);
            }

            List <ICompletionEntry> data = new List <ICompletionEntry>();

            bool        completingDotExpression = false;
            IReturnType resolvedType            = null;

            if (expressionResult.Context != ExpressionContext.Global && expressionResult.Context != ExpressionContext.TypeDeclaration)
            {
                if (expressionResult.Context == ExpressionContext.Importable &&
                    string.IsNullOrWhiteSpace(expressionResult.Expression))
                {
                    expressionResult.Expression = "Global";
                }
                else if (pressedKey == '\0')
                {
                    int idx = string.IsNullOrWhiteSpace(expressionResult.Expression)
                                                ? -1
                                                : expressionResult.Expression.LastIndexOf('.');

                    if (idx > -1)
                    {
                        expressionResult.Expression = expressionResult.Expression.Substring(0, idx);
                        // its the same as if . was pressed
                        completingDotExpression = true;
                    }
                    else
                    {
                        expressionResult.Expression = "";
                    }
                }

                var rr = resolver.Resolve(expressionResult, info, editor.Document.Text);

                if (rr == null || !rr.IsValid || (pressedKey != '.' && !completingDotExpression))
                {
                    if (((BitArray)expressionResult.Tag)[Tokens.Identifier])
                    {
                        data = new NRefactoryResolver(LanguageProperties.VBNet)
                               .CtrlSpace(editor.Caret.Line, editor.Caret.Column, info, editor.Document.Text, expressionResult.Context,
                                          ((NRefactoryCompletionItemList)result).ContainsItemsFromAllNamespaces);
                    }
                }
                else
                {
                    if (rr is MethodGroupResolveResult)
                    {
                        IMethod singleMethod = ((MethodGroupResolveResult)rr).GetMethodWithEmptyParameterList();
                        if (singleMethod != null)
                        {
                            rr = new MemberResolveResult(rr.CallingClass, rr.CallingMember, singleMethod);
                        }
                    }

                    if (rr is IntegerLiteralResolveResult && pressedKey == '.')
                    {
                        return(result);
                    }

                    data = rr.GetCompletionData(info.CompilationUnit.ProjectContent, ((NRefactoryCompletionItemList)result).ContainsItemsFromAllNamespaces) ?? new List <ICompletionEntry>();

                    resolvedType = rr.ResolvedType;
                }
            }

            bool addedKeywords = false;

            if (expressionResult.Tag != null && (expressionResult.Context != ExpressionContext.Importable) && pressedKey != '.' && !completingDotExpression)
            {
                AddVBNetKeywords(data, (BitArray)expressionResult.Tag);
                addedKeywords = true;
            }

            CodeCompletionItemProvider.ConvertCompletionData(result, data, expressionResult.Context);

            if (addedKeywords && result.Items.Any())
            {
                AddTemplates(editor, result);
            }

            string word = editor.GetWordBeforeCaret().Trim();

            IClass  c = GetCurrentClass(editor);
            IMember m = GetCurrentMember(editor);

            HandleKeyword(ref result, resolvedType, word, c, m, editor, pressedKey);

            AddSpecialItems(ref result, info, resolvedType, m, expressionResult, editor);

            char prevChar;

            if (pressedKey == '\0')               // ctrl+space
            {
                prevChar = editor.Caret.Offset > 0 ? editor.Document.GetCharAt(editor.Caret.Offset - 1) : '\0';
                word     = char.IsLetterOrDigit(prevChar) || prevChar == '_' ? editor.GetWordBeforeCaret() : "";

                if (!string.IsNullOrWhiteSpace(word))
                {
                    result.PreselectionLength = word.Length;
                }
            }

            prevChar = editor.Caret.Offset > 0 ? editor.Document.GetCharAt(editor.Caret.Offset - 1) : '\0';

            if (prevChar == '_')
            {
                result.PreselectionLength++;
            }

            result.SortItems();

            return(result);
        }
Beispiel #6
0
        public virtual CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch)
        {
            switch (ch)
            {
            case '(':
                if (enableMethodInsight && CodeCompletionOptions.InsightEnabled)
                {
                    IInsightWindow insightWindow = editor.ShowInsightWindow(new MethodInsightProvider().ProvideInsight(editor));
                    if (insightWindow != null && insightHandler != null)
                    {
                        insightHandler.InitializeOpenedInsightWindow(editor, insightWindow);
                        insightHandler.HighlightParameter(insightWindow, 0);
                    }
                    return(CodeCompletionKeyPressResult.Completed);
                }
                break;

            case '[':
                if (enableIndexerInsight && CodeCompletionOptions.InsightEnabled)
                {
                    IInsightWindow insightWindow = editor.ShowInsightWindow(new IndexerInsightProvider().ProvideInsight(editor));
                    if (insightWindow != null && insightHandler != null)
                    {
                        insightHandler.InitializeOpenedInsightWindow(editor, insightWindow);
                    }
                    return(CodeCompletionKeyPressResult.Completed);
                }
                break;

            case '<':
                if (enableXmlCommentCompletion)
                {
                    new CommentCompletionItemProvider().ShowCompletion(editor);
                    return(CodeCompletionKeyPressResult.Completed);
                }
                break;

            case '.':
                if (enableDotCompletion)
                {
                    new DotCodeCompletionItemProvider().ShowCompletion(editor);
                    return(CodeCompletionKeyPressResult.Completed);
                }
                break;

            case ' ':
                if (CodeCompletionOptions.KeywordCompletionEnabled)
                {
                    string word = editor.GetWordBeforeCaret();
                    if (!string.IsNullOrEmpty(word))
                    {
                        if (HandleKeyword(editor, word))
                        {
                            return(CodeCompletionKeyPressResult.Completed);
                        }
                    }
                }
                break;
            }
            return(CodeCompletionKeyPressResult.None);
        }
		public CodeCompletionKeyPressResult HandleKeyPress(ITextEditor editor, char ch)
		{
			if (IsInComment(editor) || IsInString(editor))
				return CodeCompletionKeyPressResult.None;
			
			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);
			}
			
			VBNetExpressionFinder ef = new VBNetExpressionFinder(ParserService.GetParseInformation(editor.FileName));
			
			ExpressionResult result;
			
			switch (ch) {
				case '(':
					if (CodeCompletionOptions.InsightEnabled) {
						IInsightWindow insightWindow = editor.ShowInsightWindow(new MethodInsightProvider().ProvideInsight(editor));
						if (insightWindow != null) {
							insightHandler.InitializeOpenedInsightWindow(editor, insightWindow);
							insightHandler.HighlightParameter(insightWindow, 0);
							insightWindow.CaretPositionChanged += delegate { Run(insightWindow, editor); };
						}
						return CodeCompletionKeyPressResult.Completed;
					}
					break;
				case ',':
					if (CodeCompletionOptions.InsightRefreshOnComma && CodeCompletionOptions.InsightEnabled) {
						IInsightWindow insightWindow;
						editor.Document.Insert(editor.Caret.Offset, ",");
						if (insightHandler.InsightRefreshOnComma(editor, ch, out insightWindow)) {
							if (insightWindow != null) {
								insightHandler.HighlightParameter(insightWindow, GetArgumentIndex(editor) + 1);
								insightWindow.CaretPositionChanged += delegate { Run(insightWindow, editor); };;
							}
						}
						return CodeCompletionKeyPressResult.EatKey;
					}
					break;
				case '\n':
					TryDeclarationTypeInference(editor, editor.Document.GetLineForOffset(editor.Caret.Offset));
					break;
				case '.':
					string w = editor.GetWordBeforeCaret(); int index = w.IndexOf('.');
					
					if (index > -1 && w.Length - index == 2)
						index = editor.Caret.Offset - 2;
					else
						index = editor.Caret.Offset;
					
					result = ef.FindExpression(editor.Document.Text, index);
					LoggingService.Debug("CC: After dot, result=" + result + ", context=" + result.Context);
					ShowCompletion(result, editor, ch);
					return CodeCompletionKeyPressResult.Completed;
				case '@':
					if (editor.Caret.Offset > 0 && editor.Document.GetCharAt(editor.Caret.Offset - 1) == '.')
						return CodeCompletionKeyPressResult.None;
					goto default;
				case ' ':
					editor.Document.Insert(editor.Caret.Offset, " ");
					result = ef.FindExpression(editor.Document.Text, editor.Caret.Offset);
					
					string word = editor.GetWordBeforeCaret().Trim();
					if (word.Equals("overrides", StringComparison.OrdinalIgnoreCase) || word.Equals("return", StringComparison.OrdinalIgnoreCase) || !LiteralMayFollow((BitArray)result.Tag) && !OperatorMayFollow((BitArray)result.Tag) && ExpressionContext.IdentifierExpected != result.Context) {
						LoggingService.Debug("CC: After space, result=" + result + ", context=" + result.Context);
						ShowCompletion(result, editor, ch);
					}
					return CodeCompletionKeyPressResult.EatKey;
				default:
					if (CodeCompletionOptions.CompleteWhenTyping) {
						int cursor = editor.Caret.Offset;
						char prevChar = cursor > 1 ? editor.Document.GetCharAt(cursor - 1) : ' ';
						char ppChar = cursor > 2 ? editor.Document.GetCharAt(cursor - 2) : ' ';
						
						result = ef.FindExpression(editor.Document.Text, cursor);
						
						if ((result.Context != ExpressionContext.IdentifierExpected && char.IsLetter(ch)) &&
						    (!char.IsLetterOrDigit(prevChar) && prevChar != '.')) {
							if (prevChar == '@' && ppChar == '.')
								return CodeCompletionKeyPressResult.None;
							if (IsTypeCharacter(ch, prevChar))
								return CodeCompletionKeyPressResult.None;
							LoggingService.Debug("CC: Beginning to type a word, result=" + result + ", context=" + result.Context);
							ShowCompletion(result, editor, ch);
							return CodeCompletionKeyPressResult.CompletedIncludeKeyInCompletion;
						}
					}
					break;
			}
			
			return CodeCompletionKeyPressResult.None;
		}