public void MakeRelativeImports(ChangeAST change, int maxNumberOfRelativeLevels) { try { if (Regex.Matches(RelativeImportPath, @"\.\.").Count <= maxNumberOfRelativeLevels) { change.ChangeNode(_pathNode, $" '{RelativeImportPath}'"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public ChangeAST AddImports(ChangeAST change, IEnumerable <ImportedModule> imports) { var groupedByPath = imports.GroupBy(i => i.Path); foreach (var byPath in groupedByPath) { var existingImportsForPath = Imports.SingleOrDefault(i => i.FilePath == byPath.Key); if (existingImportsForPath != null) { var newImports = byPath.Where(imp => !existingImportsForPath.ImportedModules.Any(i => i.Name.Equals(imp.Name))); existingImportsForPath.Add(newImports); change.ChangeNode(existingImportsForPath.ImportDeclaration, existingImportsForPath.Serialize()); } } return(change); }
public ChangeAST RemoveImports(ChangeAST change, IEnumerable <ImportedModule> imports) { var groupedByPath = imports.GroupBy(i => i.Path); foreach (var byPath in groupedByPath) { var existingImportsForPath = Imports.SingleOrDefault(i => i.FilePath == byPath.Key); if (existingImportsForPath != null) { existingImportsForPath.Remove(imports); change.Delete(existingImportsForPath.ImportDeclaration); change.ChangeNode(existingImportsForPath.ImportDeclaration, existingImportsForPath.Serialize()); } } return(change); }
private void Button_Click_4(object sender, RoutedEventArgs e) { var node = lbNodes.SelectedItem as Node; if (node != null) { try { if (_currentChangeAst == null) { _currentChangeAst = new ChangeAST(_nodeChangeItems); } _currentChangeAst.ChangeNode(node, tbNodeText.Text); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } } }
public void MakeAbsoluteImport(ChangeAST change, bool skipSelfAndSubdirectories) { try { if (skipSelfAndSubdirectories && RelativeImportPath.StartsWith("./") && !RelativeImportPath.Contains("..")) { return; } if (skipSelfAndSubdirectories && !RelativeImportPath.Contains("../")) { return; } change.ChangeNode(_pathNode, $" '{GetAbsoluteImport()}'"); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
public ChangeAST AddRemoveImports(ChangeAST change, IEnumerable <ImportedModule> toAdd, IEnumerable <ImportedModule> removedUsagesImports) { List <Imports> toChange = new List <Imports>(); var allImports = new List <ImportedModule>(); allImports.AddRange(toAdd.Select(n => new ImportedModule(n.Name, n.IsLocalImport ? n.Path.ToRelativeImportPath(this.FileName) : n.Path))); allImports.AddRange(removedUsagesImports); foreach (var byPath in allImports.GroupBy(i => i.Path)) { var existingImportsForPath = Imports.SingleOrDefault(i => i.FilePath == byPath.Key && !i.ImportedModules.Any(m => m.IsNamespaceImport)); if (existingImportsForPath != null) { var newImports = toAdd.Where(imp => !existingImportsForPath.ImportedModules.Any(i => i.Name.Equals(imp.Name))); var removedImports = removedUsagesImports.Where(imp => existingImportsForPath.ImportedModules.Any(i => i.Name.Equals(imp.Name))); existingImportsForPath.Add(newImports); existingImportsForPath.Remove(removedImports); toChange.Add(existingImportsForPath); } else { toChange.Add(new Imports(this.FileName, this.Root, byPath.ToArray())); } } var lastImport = Imports.Last(i => i.ImportDeclaration != null); foreach (var import in toChange.Where(i => i.ImportDeclaration != null)) { change.ChangeNode(import.ImportDeclaration, import.Serialize()); } foreach (var import in toChange.Where(i => i.ImportDeclaration == null)) { change.InsertAfter(lastImport.ImportDeclaration, $"\n{import.Serialize()}"); } return(change); }