public static void CreateImportStatementForCurrentCodeContext() { /* * 1) Get currently selected symbol * 2) Enum through all parse caches to find first occurrence of this symbol * 3) Find the current module's first import statement (or take a location after the module statement / take (0,0) as the statement location) * 4) Create import statement */ // 1) var doc = IdeApp.Workbench.ActiveDocument; var edData = DResolverWrapper.GetEditorData(doc); var sr = new SymbolImportRefactoring(); var name = SymbolImportRefactoring.GetSelectedSymbolRoot(edData); if (string.IsNullOrEmpty(name)) { MessageService.ShowError("No text symbol selected."); return; } // 2) var possibleNodes=SearchInCache(edData.ParseCache, name).ToList(); if (possibleNodes.Count == 0) { MessageService.ShowError("Symbol could not be found in global module range."); return; } //TODO: Choice dialog var chosenNode = possibleNodes[0]; if (chosenNode == null) return; var chosenImportModule = chosenNode.NodeRoot as IAbstractSyntaxTree; if (chosenImportModule == null) return; // 3) var insertLocation=CodeLocation.Empty; // At this location, a line break + the new import statement will be inserted foreach (var stmt in edData.SyntaxTree.StaticStatements) if (stmt is ImportStatement) insertLocation = stmt.EndLocation; if (insertLocation == CodeLocation.Empty && edData.SyntaxTree.OptionalModuleStatement != null) insertLocation = edData.SyntaxTree.OptionalModuleStatement.EndLocation; // 4) var importCode = "import " + chosenImportModule.ModuleName + ";\n"; doc.Editor.Insert(doc.Editor.GetLine(insertLocation.Line).EndOffset,importCode); }
void TryImportMissingSymbol() { SymbolImportRefactoring.CreateImportStatementForCurrentCodeContext(); }