예제 #1
0
        public override TooltipItem GetItem(TextEditor editor, int offset)
        {
            // Note: Normally, the document already should be open
            var doc = IdeApp.Workbench.GetDocument(editor.Document.FileName);

            if (doc == null || !(doc.ParsedDocument is ParsedDModule))
            {
                return(null);
            }

            // Due the first note, the AST already should exist
            var ast = (doc.ParsedDocument as ParsedDModule).DDom;

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

            // Get code cache
            var codeCache = DResolverWrapper.CreateCacheList(doc);

            // Create editor context
            var line = editor.GetLineByOffset(offset);

            var ed = new EditorData {
                CaretOffset   = offset,
                CaretLocation = new CodeLocation(offset - line.Offset, editor.OffsetToLineNumber(offset)),
                ModuleCode    = editor.Text,
                ParseCache    = codeCache,
                SyntaxTree    = ast
            };

            // Let the engine build all contents
            DResolver.NodeResolutionAttempt att;
            var rr = DResolver.ResolveTypeLoosely(ed, out att);

            // Create tool tip item
            if (rr != null)
            {
                return(new TooltipItem(rr, offset, 1));
            }
            return(null);
        }
예제 #2
0
        public static ResolutionContext CreateCurrentContext()
        {
            Document doc = null;

            DispatchService.GuiSyncDispatch(() => doc = Ide.IdeApp.Workbench.ActiveDocument);
            if (doc != null)
            {
                var ddoc = doc.ParsedDocument as ParsedDModule;
                if (ddoc != null)
                {
                    var ast = ddoc.DDom;
                    if (ast != null)
                    {
                        IStatement stmt;
                        var        caret = new D_Parser.Dom.CodeLocation(doc.Editor.Caret.Column, doc.Editor.Caret.Line);
                        var        bn    = DResolver.SearchBlockAt(ast, caret, out stmt);
                        var        dbn   = bn as DBlockNode;
                        if (stmt == null && dbn != null)
                        {
                            //TODO: If inside an expression statement, search the nearest function call or template instance expression - and try to evaluate that one.

                            if (dbn.StaticStatements.Count != 0)
                            {
                                foreach (var ss in dbn.StaticStatements)
                                {
                                    if (caret >= ss.Location && caret <= ss.EndLocation)
                                    {
                                        stmt = ss;
                                        break;
                                    }
                                }
                            }
                        }

                        var ed = DResolverWrapper.CreateEditorData(doc);
                        return(new ResolutionContext(ed.ParseCache, new ConditionalCompilationFlags(ed), bn, stmt));
                    }
                }
            }
            return(new ResolutionContext(DResolverWrapper.CreateCacheList(),
                                         new ConditionalCompilationFlags(VersionIdEvaluation.GetOSAndCPUVersions(), 1, true), null));
        }
예제 #3
0
 public static ResolutionContext CreateContext(Document doc, bool pushFirstScope = true)
 {
     if (doc != null)
     {
         var ddoc = doc.ParsedDocument as ParsedDModule;
         if (ddoc != null)
         {
             var ast = ddoc.DDom;
             if (ast != null)
             {
                 var ed = DResolverWrapper.CreateEditorData(doc);
                 if (pushFirstScope)
                 {
                     return(new ResolutionContext(ed.ParseCache, new ConditionalCompilationFlags(ed), ast, ed.CaretLocation));
                 }
                 return(new ResolutionContext(ed.ParseCache, new ConditionalCompilationFlags(ed)));
             }
         }
     }
     return(new ResolutionContext(DResolverWrapper.CreateCacheList(),
                                  new ConditionalCompilationFlags(VersionIdEvaluation.GetOSAndCPUVersions(), 1, true)));
 }
예제 #4
0
        public override List <Change> PerformChanges(RefactoringOptions options, object prop)
        {
            #region Init
            var renameProperties = prop as RenameProperties;
            if (renameProperties == null)
            {
                return(null);
            }

            var changes = new List <Change>();

            var doc = options.Document;
            if (doc == null)
            {
                return(null);
            }

            var ddoc = doc.ParsedDocument as ParsedDModule;
            if (ddoc == null)
            {
                return(null);
            }

            var n = options.SelectedItem as INode;
            if (n == null)
            {
                return(null);
            }

            var project = doc.HasProject ? doc.Project as DProject : null;

            var parseCache = DResolverWrapper.CreateCacheList(project);

            var modules = new List <DModule>();
            if (project == null)
            {
                modules.Add((Ide.IdeApp.Workbench.ActiveDocument.ParsedDocument as ParsedDModule).DDom);
            }
            else
            {
                foreach (var p in project.GetSourcePaths())
                {
                    modules.AddRange(GlobalParseCache.EnumModules(p));
                }
            }

            var ctxt = ResolutionContext.Create(parseCache, null, null);
            #endregion

            // Enumerate references
            foreach (var mod in modules)
            {
                if (mod == null)
                {
                    continue;
                }

                var references = D_Parser.Refactoring.ReferencesFinder.Scan(mod, n, ctxt).ToList();

                if (((DModule)n.NodeRoot).FileName == mod.FileName)
                {
                    references.Insert(0, new IdentifierDeclaration(n.NameHash)
                    {
                        Location = n.NameLocation
                    });
                }

                if (references.Count < 1)
                {
                    continue;
                }

                var txt             = TextFileProvider.Instance.GetEditableTextFile(new FilePath(mod.FileName));
                var prevReplacement = CodeLocation.Empty;
                foreach (ISyntaxRegion reference in references)
                {
                    if (prevReplacement == reference.Location)
                    {
                        continue;
                    }

                    prevReplacement = reference.Location;
                    changes.Add(new TextReplaceChange {
                        FileName     = mod.FileName,
                        InsertedText = renameProperties.NewName,
                        RemovedChars = n.Name.Length,
                        Description  = string.Format(GettextCatalog.GetString("Replace '{0}' with '{1}'"), n.Name, renameProperties.NewName),
                        Offset       = txt.GetPositionFromLineColumn(reference.Location.Line, reference.Location.Column)
                    });
                }
            }

            return(changes);
        }