Ejemplo n.º 1
0
        public static NRefactoryResolver CreateResolverForContext(LanguageProperties language, ITextEditor context)
        {
            NRefactoryResolver resolver = new NRefactoryResolver(language);

            resolver.Initialize(ParserService.GetParseInformation(context.FileName), context.Caret.Line, context.Caret.Column);
            return(resolver);
        }
Ejemplo n.º 2
0
        // this should use CodeGenerator and build a BlockStatement to work for both C# and VB
        string GetSwitchBodyCode(IReturnType enumType, string indent, CodeGenerator generator)
        {
            if (generator == null)
            {
                return(string.Empty);
            }

            ParseInformation parseInfo = ParserService.GetParseInformation(this.Editor.FileName);
            var visitor = new CSharpOutputVisitor();

            CodeGenerator.ConvertType(enumType, this.classFinderContext).AcceptVisitor(visitor, null);
            var           qualifiedEnumType = visitor.Text;
            StringBuilder sb    = new StringBuilder();
            bool          first = true;

            foreach (var enumCase in GetEnumCases(enumType))
            {
                string qualifiedName = qualifiedEnumType + "." + enumCase.Name;
                sb.AppendLine(string.Format((first ? "" : indent) + "case {0}:", qualifiedName));
                sb.AppendLine(indent + context.Tab);
                sb.AppendLine(indent + context.Tab + "break;");
                first = false;
            }
            sb.AppendLine(indent + "default:");
            sb.Append(string.Format(indent + context.Tab + "throw new Exception(\"Invalid value for {0}\");", enumType.Name));
            return(sb.ToString());
        }
Ejemplo n.º 3
0
        void ShowDotCompletion(string currentText)
        {
            StackFrame frame = this.process.GetCurrentExecutingFrame();

            if (frame == null)
            {
                return;
            }

            var seg = frame.NextStatement;

            if (seg == null)
            {
                return;
            }

            var expressionFinder = ParserService.GetExpressionFinder(seg.Filename);
            var info             = ParserService.GetParseInformation(seg.Filename);

            string text = ParserService.GetParseableFileContent(seg.Filename).Text;

            int currentOffset = TextEditor.Caret.Offset - console.CommandOffset - 1;

            var expr = expressionFinder.FindExpression(currentText, currentOffset);

            expr.Region = new DomRegion(seg.StartLine, seg.StartColumn, seg.EndLine, seg.EndColumn);

            var rr = resolver.Resolve(expr, info, text);

            if (rr != null)
            {
                TextEditor.ShowCompletionWindow(new DotCodeCompletionItemProvider().GenerateCompletionListForResolveResult(rr, expr.Context));
            }
        }
Ejemplo n.º 4
0
        public override ICompletionItemList GenerateCompletionList(ITextEditor editor)
        {
            ParseInformation parseInfo = ParserService.GetParseInformation(editor.FileName);

            if (parseInfo == null)
            {
                return(null);
            }
            IClass c = parseInfo.CompilationUnit.GetInnermostClass(editor.Caret.Line, editor.Caret.Column);

            if (c == null)
            {
                return(null);
            }
            LanguageProperties         language = c.ProjectContent.Language;
            OverrideCompletionItemList result   = new OverrideCompletionItemList();

            foreach (IMember m in GetOverridableMembers(c))
            {
                if (language.ShowMemberInOverrideCompletion(m))
                {
                    result.Items.Add(new OverrideCompletionItem(m));
                }
            }
            result.SortItems();
            return(result);
        }
        Variable ResolveVariable(Variable variable)
        {
            Dom.ParseInformation info = ParserService.GetParseInformation(this.textEditor.FileName);
            Dom.ExpressionResult res  = new Dom.ExpressionResult(variable.Name,
                                                                 Dom.DomRegion.FromLocation(variable.StartPos, variable.EndPos),
                                                                 Dom.ExpressionContext.Default, null);
            Dom.ResolveResult result = this.GetResolver().Resolve(res, info, this.textEditor.Document.Text);

            Dom.IReturnType type   = currentProjectContent.SystemTypes.Object;
            Dom.ClassFinder finder = new Dom.ClassFinder(currentClass, textEditor.Caret.Line, textEditor.Caret.Column);

            if (result != null && result.ResolvedType != null)
            {
                type = result.ResolvedType;
            }

            if (variable.Type.Type == "var")
            {
                variable.Type = Dom.Refactoring.CodeGenerator.ConvertType(type, finder);
            }

            variable.IsReferenceType = type.IsReferenceType == true;

            return(variable);
        }
Ejemplo n.º 6
0
        public override void Run()
        {
            IViewContent content = WorkbenchSingleton.Workbench.ActiveViewContent;

            if (content != null && content.PrimaryFileName != null && content is IEditable)
            {
                IParser p = ParserFactory.CreateParser(SupportedLanguage.VBNet, new StringReader(((IEditable)content).Text));
                p.Parse();

                if (p.Errors.Count > 0)
                {
                    MessageService.ShowError("${res:ICSharpCode.SharpDevelop.Commands.Convert.CorrectSourceCodeErrors}\n" + p.Errors.ErrorOutput);
                    return;
                }
                ICSharpCode.NRefactory.PrettyPrinter.CSharpOutputVisitor output = new ICSharpCode.NRefactory.PrettyPrinter.CSharpOutputVisitor();
                List <ISpecial> specials = p.Lexer.SpecialTracker.CurrentSpecials;
                PreprocessingDirective.VBToCSharp(specials);
                IAstVisitor v = new VBNetToCSharpConvertVisitor(ParserService.CurrentProjectContent,
                                                                ParserService.GetParseInformation(content.PrimaryFileName));
                v.VisitCompilationUnit(p.CompilationUnit, null);
                using (SpecialNodesInserter.Install(specials, output)) {
                    output.VisitCompilationUnit(p.CompilationUnit, null);
                }

                FileService.NewFile("Generated.cs", output.Text);
            }
        }
        public override ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped)
        {
            ParseInformation parseInfo = ParserService.GetParseInformation(fileName);

            if (parseInfo == null)
            {
                return(null);
            }
            IClass c = parseInfo.MostRecentCompilationUnit.GetInnermostClass(textArea.Caret.Line, textArea.Caret.Column);

            if (c == null)
            {
                return(null);
            }
            List <ICompletionData> result = new List <ICompletionData>();

            foreach (IMethod m in GetOverridableMethods(c))
            {
                result.Add(new OverrideCompletionData(m));
            }
            foreach (IProperty p in GetOverridableProperties(c))
            {
                result.Add(new OverrideCompletionData(p));
            }
            return(result.ToArray());
        }
        void AddTopLevelItems(List <object> resultItems, ITextEditor textEditor, ExpressionResult expressionResult, List <string> definitions, bool addAsSubmenu)
        {
            // Insert items at this position to get the outermost expression first, followed by the inner expressions (if any).
            int           insertIndex = resultItems.Count;
            ResolveResult rr          = ResolveExpressionAtCaret(textEditor, expressionResult);
            MenuItem      item        = null;

            if (rr is MethodGroupResolveResult)
            {
                item = MakeItem(definitions, ((MethodGroupResolveResult)rr).GetMethodIfSingleOverload());
            }
            else if (rr is MemberResolveResult)
            {
                MemberResolveResult mrr = (MemberResolveResult)rr;
                item = MakeItem(definitions, mrr.ResolvedMember);
                // Seems not to be needed, as AddItemForCurrentClassAndMethod works for indexer as well (martinkonicek)

                /*if (RefactoringService.FixIndexerExpression(expressionFinder, ref expressionResult, mrr)) {
                 *      if (item != null) {
                 *              // Insert this member
                 *              resultItems.Insert(insertIndex, item);
                 *      }
                 *      // Include menu for the underlying expression of the
                 *      // indexer expression as well.
                 *      AddTopLevelItems(textEditor, expressionResult, true);
                 * }*/
            }
            else if (rr is TypeResolveResult)
            {
                item = MakeItem(definitions, ((TypeResolveResult)rr).ResolvedClass);
            }
            else if (rr is LocalResolveResult)
            {
                bool             isDefinition = textEditor.Caret.Line == ((LocalResolveResult)rr).VariableDefinitionRegion.BeginLine;
                ParseInformation pi           = ParserService.GetParseInformation(textEditor.FileName);
                IProjectContent  pc           = null;
                if (pi != null)
                {
                    pc = pi.CompilationUnit.ProjectContent;
                }
                RefactoringMenuContext context = new RefactoringMenuContext(textEditor, expressionResult, rr, isDefinition, pc, pi.CompilationUnit);
                item        = MakeItem((LocalResolveResult)rr, context);
                insertIndex = 0;                        // Insert local variable menu item at the topmost position.
            }
            if (item != null)
            {
                if (addAsSubmenu)
                {
                    resultItems.Insert(insertIndex, item);
                }
                else
                {
                    foreach (object subItem in item.Items)
                    {
                        resultItems.Add(subItem);
                    }
                    item.Items.Clear();                     // detach resultItems from old parent
                }
            }
        }
        protected virtual void OKButtonClick(object sender, RoutedEventArgs e)
        {
            ParseInformation parseInfo = ParserService.GetParseInformation(editor.FileName);

            if (optionBindings != null)
            {
                foreach (OptionBinding binding in optionBindings)
                {
                    binding.Save();
                }
            }

            if (parseInfo != null)
            {
                LanguageProperties language = parseInfo.CompilationUnit.Language;
                IClass             current  = parseInfo.CompilationUnit.GetInnermostClass(anchor.Line, anchor.Column);

                using (editor.Document.OpenUndoGroup()) {
                    // GenerateCode could modify the document.
                    // So read anchor.Offset after code generation.
                    string code = GenerateCode(language, current) ?? "";
                    editor.Document.Insert(anchor.Offset, code);
                }
            }

            Deactivate();
        }
Ejemplo n.º 10
0
        static bool TryDeclarationTypeInference(ITextEditor editor, IDocumentLine curLine)
        {
            string lineText = editor.Document.GetText(curLine.Offset, curLine.Length);
            ILexer lexer    = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new System.IO.StringReader(lineText));

            if (lexer.NextToken().Kind != Tokens.Dim)
            {
                return(false);
            }
            if (lexer.NextToken().Kind != Tokens.Identifier)
            {
                return(false);
            }
            if (lexer.NextToken().Kind != Tokens.As)
            {
                return(false);
            }
            Token t1 = lexer.NextToken();

            if (t1.Kind != Tokens.QuestionMark)
            {
                return(false);
            }
            Token t2 = lexer.NextToken();

            if (t2.Kind != Tokens.Assign)
            {
                return(false);
            }
            string expr = lineText.Substring(t2.Location.Column);

            LoggingService.Debug("DeclarationTypeInference: >" + expr + "<");
            ResolveResult rr = ParserService.Resolve(new ExpressionResult(expr),
                                                     editor.Caret.Line,
                                                     t2.Location.Column, editor.FileName,
                                                     editor.Document.Text);

            if (rr != null && rr.ResolvedType != null)
            {
                ClassFinder   context  = new ClassFinder(ParserService.GetParseInformation(editor.FileName), editor.Caret.Line, t1.Location.Column);
                VBNetAmbience ambience = new VBNetAmbience();
                if (CodeGenerator.CanUseShortTypeName(rr.ResolvedType, context))
                {
                    ambience.ConversionFlags = ConversionFlags.None;
                }
                else
                {
                    ambience.ConversionFlags = ConversionFlags.UseFullyQualifiedTypeNames;
                }
                string typeName = ambience.Convert(rr.ResolvedType);
                using (editor.Document.OpenUndoGroup()) {
                    int offset = curLine.Offset + t1.Location.Column - 1;
                    editor.Document.Remove(offset, 1);
                    editor.Document.Insert(offset, typeName);
                }
                editor.Caret.Column += typeName.Length - 1;
                return(true);
            }
            return(false);
        }
        public override bool InsertAction(TextArea textArea, char ch)
        {
            ClassFinder context = new ClassFinder(ParserService.GetParseInformation(textArea.MotherTextEditorControl.FileName),
                                                  textArea.Caret.Line + 1, textArea.Caret.Column + 1);
            int         caretPosition = textArea.Caret.Offset;
            LineSegment line          = textArea.Document.GetLineSegment(textArea.Caret.Line);
            string      lineText      = textArea.Document.GetText(line.Offset, caretPosition - line.Offset);

            foreach (char c in lineText)
            {
                if (!char.IsWhiteSpace(c) && !char.IsLetterOrDigit(c))
                {
                    return(base.InsertAction(textArea, ch));
                }
            }
            string indentation = lineText.Substring(0, lineText.Length - lineText.TrimStart().Length);

            CodeGenerator codeGen = ParserService.CurrentProjectContent.Language.CodeGenerator;

            string text = codeGen.GenerateCode(codeGen.GetOverridingMethod(member, context), indentation);

            text = text.TrimEnd();             // remove newline from end
            textArea.Document.Replace(line.Offset, caretPosition - line.Offset, text);

            int endPos  = line.Offset + text.Length;
            int endLine = textArea.Document.GetLineNumberForOffset(endPos);

            line = textArea.Document.GetLineSegment(endLine);
            textArea.MotherTextAreaControl.JumpTo(endLine, endPos - line.Offset);
            textArea.Refresh();
            return(true);
        }
Ejemplo n.º 12
0
        public IReturnType GetTypeOfExpression(AST.Expression expr, IClass callingClass)
        {
            AST.Node        node = expr;
            AST.LexicalInfo lexInfo;
            do
            {
                if (node == null)
                {
                    return(null);
                }
                lexInfo = node.LexicalInfo;
                node    = node.ParentNode;
            } while (lexInfo == null || lexInfo.FileName == null);
            if (!Initialize(ParserService.GetParseInformation(lexInfo.FileName), lexInfo.Line, lexInfo.Column))
            {
                return(null);
            }
            if (callingClass != null)
            {
                this.callingClass = callingClass;
            }
            ResolveVisitor visitor = new ResolveVisitor(this);

            visitor.Visit(expr);
            if (visitor.ResolveResult == null)
            {
                return(null);
            }
            else
            {
                return(visitor.ResolveResult.ResolvedType);
            }
        }
Ejemplo n.º 13
0
        protected Dom.IMember GetParentMember(ICSharpCode.TextEditor.TextEditorControl textEditor, int line, int column)
        {
            Dom.ParseInformation parseInfo = ParserService.GetParseInformation(textEditor.FileName);
            if (parseInfo != null)
            {
                Dom.IClass c = parseInfo.MostRecentCompilationUnit.GetInnermostClass(line, column);
                if (c != null)
                {
                    foreach (Dom.IMember member in c.Properties)
                    {
                        if (member.BodyRegion.IsInside(line, column))
                        {
                            return(member);
                        }
                    }
                    foreach (Dom.IMember member in c.Methods)
                    {
                        if (member.BodyRegion.IsInside(line, column))
                        {
                            return(member);
                        }
                    }
                }
            }

            return(null);
        }
 void CaretPositionChanged(object sender, EventArgs e)
 {
     // ignore simple movements
     if (e != EventArgs.Empty)
     {
         return;
     }
     try {
         ParseInformation parseInfo = ParserService.GetParseInformation(textAreaControl.FileName);
         if (parseInfo != null)
         {
             if (currentCompilationUnit != (ICompilationUnit)parseInfo.MostRecentCompilationUnit)
             {
                 currentCompilationUnit = (ICompilationUnit)parseInfo.MostRecentCompilationUnit;
                 if (currentCompilationUnit != null)
                 {
                     FillClassComboBox(true);
                     FillMembersComboBox();
                 }
             }
             UpdateClassComboBox();
             UpdateMembersComboBox();
         }
     } catch (Exception ex) {
         MessageService.ShowError(ex);
     }
 }
Ejemplo n.º 15
0
        bool DoCaseCompletion(ITextEditor editor)
        {
            ITextEditorCaret   caret = editor.Caret;
            NRefactoryResolver r     = new NRefactoryResolver(LanguageProperties.CSharp);

            if (r.Initialize(ParserService.GetParseInformation(editor.FileName), caret.Line, caret.Column))
            {
                INode currentMember = r.ParseCurrentMember(editor.Document.Text);
                if (currentMember != null)
                {
                    CaseCompletionSwitchFinder ccsf = new CaseCompletionSwitchFinder(caret.Line, caret.Column);
                    currentMember.AcceptVisitor(ccsf, null);
                    if (ccsf.bestStatement != null)
                    {
                        r.RunLookupTableVisitor(currentMember);
                        ResolveResult rr = r.ResolveInternal(ccsf.bestStatement.SwitchExpression, ExpressionContext.Default);
                        if (rr != null && rr.ResolvedType != null)
                        {
                            return(ProvideContextCompletion(editor, rr.ResolvedType, ' '));
                        }
                    }
                }
            }
            return(false);
        }
Ejemplo n.º 16
0
            public override ResolveResult Resolve(ITextEditor editor, ExpressionResult expressionResult)
            {
                // bypass ParserService.Resolve and set resolver.LimitMethodExtractionUntilCaretLine
                ParseInformation   parseInfo = ParserService.GetParseInformation(editor.FileName);
                NRefactoryResolver resolver  = new NRefactoryResolver(LanguageProperties.CSharp);

                resolver.LimitMethodExtractionUntilLine = editor.Caret.Line;
                return(resolver.Resolve(expressionResult, parseInfo, editor.Document.Text));
            }
Ejemplo n.º 17
0
 protected override List <ICompletionEntry> CtrlSpace(ITextEditor editor, ExpressionContext context)
 {
     return(new BooResolver().CtrlSpace(
                editor.Caret.Line,
                editor.Caret.Column,
                ParserService.GetParseInformation(editor.FileName),
                editor.Document.Text,
                context));
 }
            protected override ResolveResult Resolve(ExpressionResult expressionResult, int caretLineNumber, int caretColumn, string fileName, string fileContent)
            {
                // bypass ParserService.Resolve and set resolver.LimitMethodExtractionUntilCaretLine
                ParseInformation   parseInfo = ParserService.GetParseInformation(fileName);
                NRefactoryResolver resolver  = new NRefactoryResolver(LanguageProperties.CSharp);

                resolver.LimitMethodExtractionUntilLine = caretLineNumber;
                return(resolver.Resolve(expressionResult, parseInfo, fileContent));
            }
Ejemplo n.º 19
0
        CodeGenerator GetCodeGenerator(EditorContext context)
        {
            var parseInfo = ParserService.GetParseInformation(context.Editor.FileName);

            if (parseInfo == null)
            {
                return(null);
            }
            return(parseInfo.CompilationUnit.Language.CodeGenerator);
        }
Ejemplo n.º 20
0
        protected override List <ICompletionEntry> CtrlSpace(ITextEditor editor, ExpressionContext context)
        {
            var resolver = new Dom.NRefactoryResolver.NRefactoryResolver(language);

            return(resolver.CtrlSpace(
                       editor.Caret.Line, editor.Caret.Column,
                       ParserService.GetParseInformation(editor.FileName),
                       editor.Document.Text,
                       context, this.ShowItemsFromAllNamespaces));
        }
Ejemplo n.º 21
0
        /// <summary>
        /// Returns CodeGenerator for C# or VB depending on the current file where snippet is being inserted.
        /// </summary>
        CodeGenerator GetCodeGeneratorForCurrentFile()
        {
            ParseInformation parseInfo = ParserService.GetParseInformation(this.Editor.FileName);

            if (parseInfo != null)
            {
                return(parseInfo.CompilationUnit.Language.CodeGenerator);
            }
            return(null);
        }
Ejemplo n.º 22
0
        protected Dom.IMember GetParentMember(ITextEditor textEditor, int line, int column)
        {
            Dom.ParseInformation parseInfo = ParserService.GetParseInformation(textEditor.FileName);
            if (parseInfo != null)
            {
                return(parseInfo.CompilationUnit.GetInnermostMember(line, column));
            }

            return(null);
        }
Ejemplo n.º 23
0
        IProjectContent GetProjectContentForFile()
        {
            ParseInformation parseInfo = ParserService.GetParseInformation(this.DesignerCodeFile.FileName);

            if (parseInfo != null)
            {
                return(parseInfo.CompilationUnit.ProjectContent);
            }
            return(DefaultProjectContent.DummyProjectContent);
        }
        InsertCtorDialog CreateDialog(InsertionContext context)
        {
            ITextEditor textEditor = context.TextArea.GetService(typeof(ITextEditor)) as ITextEditor;

            if (textEditor == null)
            {
                return(null);
            }

            IEditorUIService uiService = textEditor.GetService(typeof(IEditorUIService)) as IEditorUIService;

            if (uiService == null)
            {
                return(null);
            }

            ParseInformation parseInfo = ParserService.GetParseInformation(textEditor.FileName);

            if (parseInfo == null)
            {
                return(null);
            }

            CodeGenerator generator = parseInfo.CompilationUnit.Language.CodeGenerator;

            // cannot use insertion position at this point, because it might not be
            // valid, because we are still generating the elements.
            // DOM is not updated
            ICSharpCode.AvalonEdit.Document.TextLocation loc = context.Document.GetLocation(context.StartPosition);

            IClass current = parseInfo.CompilationUnit.GetInnermostClass(loc.Line, loc.Column);

            if (current == null)
            {
                return(null);
            }

            List <PropertyOrFieldWrapper> parameters = CreateCtorParams(current).ToList();

            if (!parameters.Any())
            {
                return(null);
            }

            ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition);

            anchor.MovementType = AnchorMovementType.BeforeInsertion;

            InsertCtorDialog dialog = new InsertCtorDialog(context, textEditor, anchor, current, parameters);

            dialog.Element = uiService.CreateInlineUIElement(anchor, dialog);

            return(dialog);
        }
        void UpdateClassComboBox()
        {
            // Still needed ?
            if (currentCompilationUnit == null)
            {
                currentCompilationUnit = (ICompilationUnit)ParserService.GetParseInformation(FileUtility.NormalizePath(textAreaControl.FileName)).MostRecentCompilationUnit;
            }

            autoselect = false;
            try {
                if (currentCompilationUnit != null)
                {
                    //// Alex: when changing between files in different compilation units whole process must be restarted
                    //// happens usually when files are opened from different project(s)
                    for (int i = 0; i < classComboBox.Items.Count; ++i)
                    {
                        if (((ComboBoxItem)classComboBox.Items[i]).IsInside(textAreaControl.ActiveTextAreaControl.Caret.Line))
                        {
                            bool innerClassContainsCaret = false;
                            for (int j = i + 1; j < classComboBox.Items.Count; ++j)
                            {
                                if (((ComboBoxItem)classComboBox.Items[j]).IsInside(textAreaControl.ActiveTextAreaControl.Caret.Line))
                                {
                                    innerClassContainsCaret = true;
                                    break;
                                }
                            }
                            if (!innerClassContainsCaret)
                            {
                                if (classComboBox.SelectedIndex != i)
                                {
                                    classComboBox.SelectedIndex = i;
                                    FillMembersComboBox();
                                }
                                if (!classComboBoxSelectedMember)
                                {
                                    classComboBox.Refresh();
                                }
                                classComboBoxSelectedMember = true;
                                return;
                            }
                        }
                    }
                }
                if (classComboBoxSelectedMember)
                {
                    classComboBox.Refresh();
                    classComboBoxSelectedMember = false;
                }
            } finally {
                autoselect = true;
            }
//				classComboBox.SelectedIndex = -1;
        }
Ejemplo n.º 26
0
        ResolveResult Resolve(string prog, ExpressionResult er, string marker)
        {
            Register(prog);
            int line, column;

            GetPos(prog, marker, out line, out column);
            er.Region = new DomRegion(line, column);

            BooResolver r = new BooResolver();

            return(r.Resolve(er, ParserService.GetParseInformation(fileName), prog));
        }
 static ResolveResult ResolveExpressionAtCaret(ITextEditor textArea, ExpressionResult expressionResult)
 {
     if (expressionResult.Expression != null)
     {
         if (expressionResult.Region.IsEmpty)
         {
             expressionResult.Region = new DomRegion(textArea.Caret.Line, textArea.Caret.Column);
         }
         return(resolver.Resolve(expressionResult, ParserService.GetParseInformation(textArea.FileName), textArea.Document.Text));
     }
     return(null);
 }
Ejemplo n.º 28
0
        public bool InsightRefreshOnComma(ITextEditor editor, char ch, out IInsightWindow insightWindow)
        {
            // Show MethodInsightWindow or IndexerInsightWindow
            NRefactoryResolver r = new NRefactoryResolver(languageProperties);
            Location           cursorLocation = editor.Caret.Position;

            if (r.Initialize(ParserService.GetParseInformation(editor.FileName), cursorLocation.Y, cursorLocation.X))
            {
                TextReader currentMethod = r.ExtractCurrentMethod(editor.Document.Text);
                if (currentMethod != null)
                {
                    ILexer        lexer = ParserFactory.CreateLexer(language, currentMethod);
                    Token         token;
                    InspectedCall call = new InspectedCall(Location.Empty, null);
                    call.parent = call;
                    while ((token = lexer.NextToken()) != null && token.Kind != eofToken && token.Location < cursorLocation)
                    {
                        if (token.Kind == commaToken)
                        {
                            call.commas.Add(token.Location);
                        }
                        else if (token.Kind == openParensToken || token.Kind == openBracketToken || token.Kind == openBracesToken)
                        {
                            call = new InspectedCall(token.Location, call);
                        }
                        else if (token.Kind == closeParensToken || token.Kind == closeBracketToken || token.Kind == closeBracesToken)
                        {
                            call = call.parent;
                        }
                    }
                    int offset = LocationToOffset(editor, call.start);
                    if (offset >= 0 && offset < editor.Document.TextLength)
                    {
                        char c = editor.Document.GetCharAt(offset);
                        if (c == '(' || c == '[')
                        {
                            var insightProvider = new MethodInsightProvider {
                                LookupOffset = offset
                            };
                            var insightItems = insightProvider.ProvideInsight(editor);
                            insightWindow = ShowInsight(editor, insightItems, ResolveCallParameters(editor, call), ch);
                            return(insightWindow != null);
                        }
                        else
                        {
                            Core.LoggingService.Warn("Expected '(' or '[' at start position");
                        }
                    }
                }
            }
            insightWindow = null;
            return(false);
        }
        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);
        }
Ejemplo n.º 30
0
        static IClass GetCurrentClass(ITextEditor editor)
        {
            var caret            = editor.Caret;
            NRefactoryResolver r = new NRefactoryResolver(LanguageProperties.VBNet);

            if (r.Initialize(ParserService.GetParseInformation(editor.FileName), caret.Line, caret.Column))
            {
                return(r.CallingClass);
            }
            else
            {
                return(null);
            }
        }