public override void Execute() { CodeClass codeClass = _target as CodeClass; if (codeClass == null && !ReferenceUtil.HaveAClass(_target, out codeClass)) { return; } if (codeClass != null) { TextPoint tp = codeClass.StartPoint; EditPoint ep = tp.CreateEditPoint(); ep.StartOfDocument(); int lastUsing = -1; string keyword = string.Empty; string patternText = string.Empty; switch (codeClass.Language) { case CodeModelLanguageConstants.vsCMLanguageCSharp: keyword = "using"; patternText = String.Concat(keyword, " {0};"); break; case CodeModelLanguageConstants.vsCMLanguageVB: keyword = "Imports"; patternText = String.Concat(keyword, " {0}"); break; default: throw new NotSupportedException("Language not supported"); } string usingText = String.Format(patternText, _usingNamespace); //string usingText = String.Format("using {0}", usingNamespace); while (!ep.AtEndOfDocument) { int length = ep.LineLength; string line = ep.GetText(ep.LineLength); //if (line.Contains("using")) return; if (line.Contains(usingText)) { return; } if (line.StartsWith(keyword)) { lastUsing = ep.Line; } ep.LineDown(1); } ep.StartOfDocument(); if (lastUsing > 0) { ep.LineDown(lastUsing); } ep.Insert(usingText); //ep.Insert(";"); ep.Insert(Environment.NewLine); if (ep.LineLength != 0) { ep.Insert(Environment.NewLine); } } }
public override void Execute() { CodeClass codeClass = _target as CodeClass; // Determine which using sentence to add Project prjContainer = codeClass.ProjectItem.ContainingProject; ProjectItem constantsFolder = DteHelperEx.FindItemByName(prjContainer.ProjectItems, "Constants", false); string usingNamespace = constantsFolder == null ? GlobalUsingNamespace : LocalUsingNamespace; if (codeClass == null && !ReferenceUtil.HaveAClass(_target, out codeClass)) { return; } if (codeClass != null) { TextPoint tp = codeClass.StartPoint; EditPoint ep = tp.CreateEditPoint(); ep.StartOfDocument(); int lastUsing = -1; string keyword = string.Empty; string patternText = string.Empty; switch (codeClass.Language) { case CodeModelLanguageConstants.vsCMLanguageCSharp: keyword = "using"; patternText = String.Concat(keyword, " {0};"); break; case CodeModelLanguageConstants.vsCMLanguageVB: keyword = "Imports"; patternText = String.Concat(keyword, " {0}"); break; default: throw new NotSupportedException("Language not supported"); } //string usingText = String.Format("using {0}", usingNamespace); string usingText = String.Format(patternText, usingNamespace); while (!ep.AtEndOfDocument) { int length = ep.LineLength; string line = ep.GetText(ep.LineLength); if (line.Contains(usingText)) { return; } //if (line.StartsWith("using")) lastUsing = ep.Line; if (line.StartsWith(keyword)) { lastUsing = ep.Line; } ep.LineDown(1); } ep.StartOfDocument(); if (lastUsing > 0) { ep.LineDown(lastUsing); } ep.Insert(usingText); //ep.Insert(";"); ep.Insert(Environment.NewLine); if (ep.LineLength != 0) { ep.Insert(Environment.NewLine); } } }