public void GetReferences(string filename, string tok, uint line, uint idx, string expr) { filename = normalizePath(filename); var ast = GetModule(filename); if (ast == null) { throw new COMException("module not found", 1); } _request = Request.References; _result = "__pending__"; Action dg = () => { _setupEditorData(); CodeLocation loc = new CodeLocation((int)idx + 1, (int)line); _editorData.CaretLocation = loc; _editorData.SyntaxTree = ast as DModule; _editorData.ModuleCode = _sources[filename]; _editorData.CaretOffset = getCodeOffset(_editorData.ModuleCode, loc); ISyntaxRegion sr = DResolver.GetScopedCodeObject(_editorData); LooseResolution.NodeResolutionAttempt attempt; var rr = sr != null?LooseResolution.ResolveTypeLoosely(_editorData, sr, out attempt, true) : null; StringBuilder refs = new StringBuilder(); if (rr != null) { var n = DResolver.GetResultMember(rr, true); if (n != null) { var ctxt = ResolutionContext.Create(_editorData, true); if (n.ContainsAttribute(DTokens.Private) || ((n is DVariable) && (n as DVariable).IsLocal)) { GetReferencesInModule(ast, refs, n, ctxt); } else { foreach (var basePath in _imports.Split(nlSeparator, StringSplitOptions.RemoveEmptyEntries)) { foreach (var mod in GlobalParseCache.EnumModulesRecursively(basePath)) { GetReferencesInModule(mod, refs, n, ctxt); } } } } //var res = TypeReferenceFinder.Scan(_editorData, System.Threading.CancellationToken.None, null); } if (!_editorData.CancelToken.IsCancellationRequested && _request == Request.References) { _result = refs.ToString(); } }; }
public void GetReferences(string filename, string tok, uint line, uint idx, string expr) { var ast = GetModule(filename); if (ast == null) { throw new COMException("module not found", 1); } _setupEditorData(); CodeLocation loc = new CodeLocation((int)idx + 1, (int)line); _editorData.CaretLocation = loc; _editorData.SyntaxTree = ast as DModule; _editorData.ModuleCode = _sources[filename]; _editorData.CaretOffset = getCodeOffset(_editorData.ModuleCode, loc); _references = null; ISyntaxRegion sr; DResolver.NodeResolutionAttempt attempt; var rr = DResolver.ResolveTypeLoosely(_editorData, out attempt, out sr); StringBuilder refs = new StringBuilder(); if (rr != null) { var n = DResolver.GetResultMember(rr, true); if (n != null) { var ctxt = ResolutionContext.Create(_editorData, true); if (n.ContainsAttribute(DTokens.Private) || ((n is DVariable) && (n as DVariable).IsLocal)) { GetReferencesInModule(ast, refs, n, ctxt); } else { foreach (var basePath in _imports.Split('\n')) { foreach (var mod in GlobalParseCache.EnumModulesRecursively(basePath)) { GetReferencesInModule(mod, refs, n, ctxt); } } } } //var res = TypeReferenceFinder.Scan(_editorData, System.Threading.CancellationToken.None, null); } _references = refs; }
private void ParseTaskFinished(ParsingFinishedEventArgs ea) { Console.WriteLine("Parse task finished: " + ea.Directory); if (!ea.Directory.StartsWith(_subFolder)) { Assert.Warn("Received ParseTaskFinished-Event for wrong directory (" + ea.Directory + ")"); return; } var moduleFiles = GlobalParseCache.EnumModulesRecursively(_subFolder).ConvertAll(module => module.FileName); CollectionAssert.AreEquivalent(moduleFiles, ModuleFileNames); _parseFinishedSemaphore.Set(); }
static IEnumerable <SearchResult> FindReferences( AbstractDProject project, INode member, ISearchProgressMonitor monitor = null) { var searchResults = new List <SearchResult>(); var parseCache = project != null ? project.ParseCache : DCompilerService.Instance.GetDefaultCompiler().GenParseCacheView(); var modules = new List <DModule>(); if (project != null) { foreach (var p in project.GetSourcePaths(IdeApp.Workspace.ActiveConfiguration)) { modules.AddRange(GlobalParseCache.EnumModulesRecursively(p, null)); } } else { modules.Add((IdeApp.Workbench.ActiveDocument.ParsedDocument as MonoDevelop.D.Parser.ParsedDModule).DDom); } if (monitor != null) { monitor.BeginStepTask("Scan for references", modules.Count, 1); } List <ISyntaxRegion> references = null; var ctxt = ResolutionContext.Create(parseCache, null); foreach (var mod in modules) { if (mod == null) { continue; } try { references = ReferencesFinder.Scan(mod, member, ctxt).ToList(); if (references.Count < 1) { if (monitor != null) { monitor.Step(1); } continue; } // Sort the references by code location references.Sort(new IdLocationComparer()); // Get actual document code var targetDoc = TextFileProvider.Instance.GetTextEditorData(new FilePath(mod.FileName)); foreach (var reference in references) { CodeLocation loc; if (reference is AbstractTypeDeclaration) { loc = ((AbstractTypeDeclaration)reference).NonInnerTypeDependendLocation; } else if (reference is IExpression) { loc = reference.Location; } else { continue; } searchResults.Add(new SearchResult(new FileProvider(mod.FileName, project), targetDoc.LocationToOffset(loc.Line, loc.Column), member.Name.Length)); } } catch (Exception ex) { LoggingService.LogWarning("Error during reference search", ex); } if (monitor != null) { monitor.Step(1); } } if (monitor != null) { monitor.EndTask(); } return(searchResults); }
public static void Main(string[] args) { /* * var sw2 = new Stopwatch(); * var code = File.ReadAllText(@"B:\Programs\D\dmd2\src\phobos\std\datetime.d"); * sw2.Start(); * var ast = DParser.ParseString(code, true); * sw2.Stop(); * Console.WriteLine (sw2.ElapsedMilliseconds); * return;*/ (new ResolutionTests()).Unqual(); (new EvaluationTests()).IsExpressionAlias(); return; // Indent testing /*var code = @" * "; * var line = 4; * var ind = D_Parser.Formatting.Indent.IndentEngineWrapper.CalculateIndent(code, line, false, 4); * var o = DocumentHelper.LocationToOffset(code, line,1); * * var o2 = o; * while(o2 < code.Length && code[o2] == ' ' || code[o2] == '\t') * o2++; * * code = code.Substring(0,o) + ind + code.Substring(o2); * Console.Write(code+"|"); * * Console.ReadKey(true); * return;*/ // Phobos & Druntime parsing Console.WriteLine("Begin parsing..."); var dirs = Environment.OSVersion.Platform == PlatformID.Unix ? new[] { @"/usr/include/dlang" } : new[] { @"B:\Programs\D\dmd2\src\phobos", @"B:\Programs\D\dmd2\src\druntime\import" }; var dirsLeft = dirs.Length; var ev = new System.Threading.AutoResetEvent(false); GlobalParseCache.BeginAddOrUpdatePaths(dirs, false, (pc) => { Console.WriteLine("{1}ms", pc.Directory, pc.ParseDuration); ev.Set(); }); ev.WaitOne(); Console.WriteLine("done."); Console.WriteLine(); var pcw = new ParseCacheView(dirs); var ccf = new ConditionalCompilationFlags(new[] { Environment.OSVersion.Platform == PlatformID.Unix ?"Posix":"Windows", "D2" }, 1, true, null, 0); Console.WriteLine("Dump parse errors:"); foreach (var dir in dirs) { foreach (var mod in GlobalParseCache.EnumModulesRecursively(dir)) { if (mod.ParseErrors.Count > 0) { Console.WriteLine(" " + mod.FileName); Console.WriteLine(" (" + mod.ModuleName + ")"); foreach (var err in mod.ParseErrors) { Console.WriteLine("({0}):", err.Location.ToString()); Console.WriteLine("\t" + err.Message); } } } } Console.WriteLine(); Console.Write("Press any key to continue . . . "); Console.ReadKey(true); }
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 ast = doc.GetDAst(); if (ast == null) { return(null); } var n = options.SelectedItem as INode; if (n == null) { return(null); } var project = doc.HasProject ? doc.Project as AbstractDProject : null; var parseCache = DResolverWrapper.CreateParseCacheView(project); var modules = new List <DModule>(); if (project == null) { modules.Add(ast); } else { foreach (var p in project.GetSourcePaths()) { modules.AddRange(GlobalParseCache.EnumModulesRecursively(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.SearchModuleForASTNodeReferences(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); }