private void LoadChild(CodeElement item) { switch (item.Kind) { case vsCMElement.vsCMElementImportStmt: if (item is CodeImport codeImport && codeImport.Namespace == CodeSiteName) { CodeSiteCodeImport = codeImport; } break; case vsCMElement.vsCMElementClass: case vsCMElement.vsCMElementProperty: case vsCMElement.vsCMElementNamespace: case vsCMElement.vsCMElementStruct: case vsCMElement.vsCMElementFunction: var itemViewModel = new CodeElementViewModel(item); if (itemViewModel.Any() || itemViewModel.IsCodeFunction) { Add(itemViewModel); } break; default: break; } }
IEnumerable <CodeImport> ParseCSV(string filename) { List <CodeImport> codes = new List <CodeImport>(); string path = Path.Combine(ResourcesFolder, filename); using (var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(path)) { parser.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited; parser.SetDelimiters(","); parser.TrimWhiteSpace = true; //get the headers string[] headers = parser.ReadFields(); int lineNumber = 1; while (parser.EndOfData == false) { CodeImport code = new CodeImport(); string[] line = parser.ReadFields(); for (int i = 0; i < line.Length; i++) { switch (headers[i].ToUpperInvariant()) { case "CRITERIAINDEX": try { code.CriteriaIndex = Convert.ToInt32(line[i]); }catch { throw new FormatException($"Unable to convert Criteria Index to integer on line { lineNumber }. Value: \"{ line[i] }\""); } break; case "CODETYPE": code.CodeType = line[i]; break; case "CODE": code.Code = line[i]; break; case "EXACTMATCH": code.SearchMethodType = string.Equals("1", line[i], StringComparison.OrdinalIgnoreCase) ? DTO.Enums.TextSearchMethodType.ExactMatch : DTO.Enums.TextSearchMethodType.StartsWith; break; } } if (code.IsEmpty() == false) { codes.Add(code); } lineNumber++; } } return(codes.OrderBy(c => c.CriteriaIndex).ThenBy(c => c.CodeType).ThenBy(c => c.SearchMethodType)); }
/// <summary> /// Check the given file for multiply added import statements /// </summary> private void CheckForDuplicateUsings(string path) { ProjectItem projectItem = Agent.GetDTE().Solution.FindProjectItem(path); if (projectItem.GetFileType() == FILETYPE.ASPX) { return; } HashSet <string> namespaces = new HashSet <string>(); bool fileOpened; foreach (CodeElement codeElement in projectItem.GetCodeModel(true, true, out fileOpened).CodeElements) { if (codeElement.Kind != vsCMElement.vsCMElementImportStmt) { continue; } CodeImport import = (CodeImport)codeElement; if (namespaces.Contains(import.Namespace)) { Assert.Fail(); } namespaces.Add(import.Namespace); } if (fileOpened) { DocumentViewsManager.CloseFile(path); } }
private async void ImportFont(object sender, RoutedEventArgs e) { FileOpenPicker picker = new FileOpenPicker { ViewMode = PickerViewMode.Thumbnail, SuggestedStartLocation = PickerLocationId.DocumentsLibrary }; picker.FileTypeFilter.Add(".h"); StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { this.Items.Clear(); CodeImport fontFile = new CodeImport(file); this.CurrentFile = await fontFile.Import(); this.CreateGrid(this.CurrentFile); this.Items.AddRange(this.CurrentFile.Items); this.FontName = file.Name; this.StorageFile = null; this.RaisePropertyChanged(nameof(this.Items)); } }
private CodeImport GetImport(CodeElements codeElements) { List <CodeElement2> elements = codeElements.Cast <CodeElement2>().ToList(); CodeImport imp = elements.FirstOrDefault(codeElement => codeElement.Kind == vsCMElement.vsCMElementIDLImport) as CodeImport; return(imp); }
public void GetStartPoint_Navigate() { CodeImport import = GetCodeImport(2); TextPoint startPoint = import.GetStartPoint(vsCMPart.vsCMPartNavigate); Assert.Equal(2, startPoint.Line); Assert.Equal(13, startPoint.LineCharOffset); }
public void GetStartPoint_WholeWithAttributes() { CodeImport import = GetCodeImport(2); TextPoint startPoint = import.GetStartPoint(vsCMPart.vsCMPartWholeWithAttributes); Assert.Equal(2, startPoint.Line); Assert.Equal(1, startPoint.LineCharOffset); }
protected override bool TryGetItemByName(string name, out EnvDTE.CodeElement element) { var node = LookupNode(); var parentElement = !IsRootNamespace ? (AbstractCodeElement)Parent : null; // Option statements foreach (var child in CodeModelService.GetOptionNodes(node)) { string childName; int ordinal; CodeModelService.GetOptionNameAndOrdinal(node, child, out childName, out ordinal); if (childName == name) { element = CodeOptionsStatement.Create(State, FileCodeModel, childName, ordinal); return(true); } } // Imports/using statements foreach (var child in CodeModelService.GetImportNodes(node)) { var childName = CodeModelService.GetImportNamespaceOrType(child); if (childName == name) { element = CodeImport.Create(State, FileCodeModel, parentElement, childName); return(true); } } // Attributes foreach (var child in CodeModelService.GetAttributeNodes(node)) { string childName; int ordinal; CodeModelService.GetAttributeNameAndOrdinal(node, child, out childName, out ordinal); if (childName == name) { element = (EnvDTE.CodeElement)CodeAttribute.Create(State, FileCodeModel, parentElement, childName, ordinal); return(true); } } // Members foreach (var child in CodeModelService.GetLogicalSupportedMemberNodes(node)) { var childName = CodeModelService.GetName(child); if (childName == name) { element = FileCodeModel.CreateCodeElement <EnvDTE.CodeElement>(child); return(true); } } element = null; return(false); }
public async Task EndPoint() { CodeImport import = await GetCodeImportAsync(2); TextPoint endPoint = import.EndPoint; Assert.Equal(2, endPoint.Line); Assert.Equal(25, endPoint.LineCharOffset); }
public async Task StartPoint() { CodeImport import = await GetCodeImportAsync(2); TextPoint startPoint = import.StartPoint; Assert.Equal(2, startPoint.Line); Assert.Equal(1, startPoint.LineCharOffset); }
public void StartPoint() { CodeImport import = GetCodeImport(2); TextPoint startPoint = import.StartPoint; Assert.Equal(2, startPoint.Line); Assert.Equal(1, startPoint.LineCharOffset); }
private static bool IsNamespaceImportPresent(CodeElement codeElement, string productNamespace) { if (codeElement.Kind.Equals(vsCMElement.vsCMElementImportStmt)) { CodeImport codeImport = (CodeImport)codeElement; return(String.Equals(codeImport.Namespace, productNamespace, StringComparison.OrdinalIgnoreCase)); } return(false); }
public async Task GetEndPoint_WholeWithAttributes() { CodeImport import = await GetCodeImportAsync(2); TextPoint endPoint = import.GetEndPoint(vsCMPart.vsCMPartWholeWithAttributes); Assert.Equal(2, endPoint.Line); Assert.Equal(25, endPoint.LineCharOffset); }
public async Task GetEndPoint_Navigate() { CodeImport import = await GetCodeImportAsync(2); TextPoint endPoint = import.GetEndPoint(vsCMPart.vsCMPartNavigate); Assert.Equal(2, endPoint.Line); Assert.Equal(24, endPoint.LineCharOffset); }
private EnvDTE.CodeElement CreateCodeImport( SyntaxNode node, AbstractCodeElement parentElement ) { var name = CodeModelService.GetImportNamespaceOrType(node); return(CodeImport.Create(this.State, this.FileCodeModel, parentElement, name)); }
public void EndPoint() { CodeImport import = GetCodeImport(2); TextPoint endPoint = import.EndPoint; Assert.Equal(2, endPoint.Line); Assert.Equal(25, endPoint.LineCharOffset); }
public void CodeElements_UnknownNamespaceInFile_ReturnsOneCodeImport() { string code = "using Tests.CodeModel;"; CreateProjectWithOneFile(code); CreateFileCodeModel(); CodeImport import = GetFirstCodeImportFromCodeElements(); Assert.AreEqual("Tests.CodeModel", import.Namespace); }
public void CodeElements_OneNamespaceAliasInFile_ReturnsOneCodeImport() { CreateProjectWithOneFile(); CreateCompilationUnitForFileProjectItem(); CreateFileCodeModel(); AddNamespaceAliasToCompilationUnit("TCM", "Test.CodeModel"); CodeImport import = GetFirstCodeImportFromCodeElements(); Assert.AreEqual("Test.CodeModel", import.Namespace); }
public void CodeElements_OneNamespaceInFile_FileNameUsedToGetCompilationUnit() { CreateProjectWithOneFile(@"d:\projects\MyProject\MyFile.cs"); CreateCompilationUnitForFileProjectItem(); CreateFileCodeModel(); AddNamespaceToCompilationUnit("Test.CodeModel"); CodeImport import = GetFirstCodeImportFromCodeElements(); Assert.AreEqual(@"d:\projects\MyProject\MyFile.cs", fakeFileService.FileNamePassedToGetCompilationUnit); }
public void CodeElements_OneNamespaceInFile_ReturnsOneCodeImport() { string code = "using System.Security;"; CreateProjectWithOneFile(code); CreateFileCodeModel(); CodeImport import = GetFirstCodeImportFromCodeElements(); Assert.AreEqual("System.Security", import.Namespace); Assert.AreEqual(global::EnvDTE.vsCMElement.vsCMElementImportStmt, import.Kind); }
private void ExpandSubCodeElement(CodeElement codeElement) { if (codeElement.Kind == vsCMElement.vsCMElementNamespace) { codeNamespace = (CodeNamespace)codeElement; } if (codeElement.Kind == vsCMElement.vsCMElementClass) { codeClass = (CodeClass2)codeElement; } if (codeElement.Kind == vsCMElement.vsCMElementFunction) { CodeFunction2 codeFunction = (CodeFunction2)codeElement; if (codeFunction.Name == "LoadContent") { loadContentFunction = codeFunction; } else if ((!open && codeFunction.Name == "Game1") /*ngikut template*/ || (open && codeFunction.Name == name)) { constructorFunction = codeFunction; } else if (codeFunction.Name == "Draw") { drawFunction = codeFunction; } else if (codeFunction.Name == "Update") { updateFunction = codeFunction; } } if (codeElement.Kind == vsCMElement.vsCMElementImportStmt) { lastImportStatement = (CodeImport)codeElement; if (((CodeImport)codeElement).Namespace == "XleModel") { xleModelImportStmtExist = true; } if (((CodeImport)codeElement).Namespace == "Jitter") { jitterImportStmtExist = true; } if (((CodeImport)codeElement).Namespace == "Jitter.Collision") { jitterCollisionImportStmtExist = true; } } foreach (CodeElement child in codeElement.Children) { ExpandSubCodeElement(child); } }
/// <summary> /// Gets the imports used in the project item. /// </summary> /// <param name="projectItem"></param> /// <returns>The imports used in the project item.</returns> public static IList <CodeImport> GetImports(ProjectItem projectItem) { List <CodeImport> imports = new List <CodeImport>(); foreach (CodeElement codeElement in projectItem.FileCodeModel.CodeElements) { CodeImport codeImport = codeElement as CodeImport; if (codeImport != null) { imports.Add(codeImport); } } return(imports); }
/// <summary> /// Adds all import statements from list /// </summary> private static void AddUsedNamespacesToList(CodeElements elements, NamespacesList list) { if (elements == null) { return; } foreach (CodeElement2 element in elements) { if (element.Kind == vsCMElement.vsCMElementImportStmt) { CodeImport codeImport = (CodeImport)element; list.Add(codeImport.Namespace, codeImport.Alias, true); } } }
/// <summary> /// Gets the using statements. /// </summary> /// <param name="instance">The instance.</param> /// <returns>A list of using statements.</returns> public static IEnumerable <string> GetUsingStatements(this ProjectItem instance) { TraceService.WriteLine("ProjectItemExtensions::GetUsingStatements in file " + instance.Name); List <string> statements = new List <string>(); foreach (CodeElement codeElement in instance.FileCodeModel.CodeElements) { CodeImport import = codeElement as CodeImport; switch (codeElement.Kind) { case vsCMElement.vsCMElementImportStmt: if (import != null) { EditPoint startEditPoint = import.GetStartPoint().CreateEditPoint(); TextPoint textPoint = import.GetEndPoint(); string text = startEditPoint.GetText(textPoint); statements.Add(text); } break; case vsCMElement.vsCMElementNamespace: foreach (CodeElement childCodeElement in codeElement.Children) { import = childCodeElement as CodeImport; if (import != null) { EditPoint startEditPoint = import.GetStartPoint().CreateEditPoint(); TextPoint textPoint = import.GetEndPoint(); string text = startEditPoint.GetText(textPoint); statements.Add(text); } } break; } } return(statements); }
/// <summary> /// Gets the namepase used from the project item. /// </summary> /// <param name="projectItem"></param> /// <returns>The namepase used from the project item.</returns> public static CodeNamespace GetNamespace(ProjectItem projectItem) { CodeNamespace codeNamespace = null; if (projectItem != null && projectItem.FileCodeModel != null && projectItem.FileCodeModel.CodeElements != null) { foreach (CodeElement codeElement in projectItem.FileCodeModel.CodeElements) { CodeImport codeImport = codeElement as CodeImport; codeNamespace = codeElement as CodeNamespace; if (codeNamespace != null) { return(codeNamespace); } } } return(codeNamespace); }
/// <summary> /// Adds the using statement. /// </summary> /// <param name="instance">The instance.</param> /// <param name="usingStatement">The using statement.</param> public static void AddUsingStatement( this ProjectItem instance, string usingStatement) { FileCodeModel2 fileCodeModel2 = instance.GetFirstNameSpace().ProjectItem.FileCodeModel as FileCodeModel2; if (fileCodeModel2 != null) { foreach (CodeElement codeElement in fileCodeModel2.CodeElements) { if (codeElement.Kind == vsCMElement.vsCMElementImportStmt) { CodeImport codeImport = codeElement as CodeImport; if (codeImport.Namespace == usingStatement) { return; } } } fileCodeModel2.AddImport(usingStatement); } }
private void AddUsingStatement(FileCodeModel model, CodeElements codeElements, string usingStatement) { bool usingStatementFound = false; CodeImport lastCodeElement = null; foreach (CodeElement codeElement in codeElements) { if (codeElement.Kind == vsCMElement.vsCMElementImportStmt) { CodeImport codeImport = codeElement as CodeImport; if (codeImport.Namespace == usingStatement) { usingStatementFound = true; } lastCodeElement = codeImport; } AddUsingStatement(model, codeElement.Children, usingStatement); } if (!usingStatementFound) { if (lastCodeElement != null) { //FileCodeModel2 model2 = model as FileCodeModel2; //model2.AddImport(usingStatement); EditPoint2 editPoint = (EditPoint2)lastCodeElement.GetEndPoint().CreateEditPoint(); editPoint.InsertNewLine(1); editPoint.Indent(null, 1); editPoint.Insert("using " + usingStatement + ";"); Helpers.LogMessage(model.DTE, model.DTE, Helpers.GetFullPathOfProjectItem(lastCodeElement.ProjectItem) + ": Added using statement '" + usingStatement + "'"); } } }
internal ShellCodeImport(CodeImport import) : base(import as CodeElement2) { _import = import; }
void CreateCodeImport(string namespaceName) { helper = new UsingHelper(); helper.AddNamespace(namespaceName); codeImport = new CodeImport(helper.Using); }
private void CheckAndAddAlias(string nameSpace, string className, string aliasName) { string resourceAlias1 = $"using {aliasName} = ", resourceAlias2 = $"global::{nameSpace}.{className}"; if (!m_IsCSharp) { resourceAlias1 = $"Imports {aliasName} = "; resourceAlias2 = $"{nameSpace}.{className}"; } //if CodeElements elements = m_TextDocument.Parent.ProjectItem.FileCodeModel.CodeElements; CodeElement lastElement = null; bool isImport = false; #region find alias or last using/Import element (return if alias found) foreach (CodeElement element in elements) //not really fast but more safe { if (!isImport) { //find first using/import statement if (element.Kind == vsCMElement.vsCMElementImportStmt) { isImport = true; } } else { //using/import statement was available so find next NON using/import statement if (element.Kind != vsCMElement.vsCMElementImportStmt) { break; } } //else if (element.Kind == vsCMElement.vsCMElementOptionStmt) { //save last option statement lastElement = element; } else if (element.Kind == vsCMElement.vsCMElementImportStmt) { //save last using/import statement lastElement = element; //check if resource alias is already there CodeImport importElement = element as CodeImport; if ((importElement.Alias != null) && importElement.Alias.Equals(aliasName) && importElement.Namespace.Equals(resourceAlias2)) { return; } } //if } //foreach #endregion EditPoint insertPoint = null; if (lastElement == null) { insertPoint = m_TextDocument.CreateEditPoint(m_TextDocument.StartPoint); //beginning of text } else { //behind last element insertPoint = lastElement.EndPoint.CreateEditPoint(); insertPoint.LineDown(1); insertPoint.StartOfLine(); if (lastElement.Kind == vsCMElement.vsCMElementOptionStmt) { insertPoint.Insert(Environment.NewLine); } } //else if (m_IsCSharp) { resourceAlias2 += ";"; } string alias = resourceAlias1 + resourceAlias2 + Environment.NewLine; insertPoint.Insert(alias); }
void CreateCodeImportWithNoNamespace() { helper = new UsingHelper(); codeImport = new CodeImport(helper.Using); }