Exemplo n.º 1
0
        protected override Action <ITextControl> ExecutePsiTransaction(ISolution solution, IProgressIndicator progress)
        {
            return(textControl =>
            {
                var psiFiles = solution.GetComponent <IPsiFiles>();
                var psiServices = solution.GetComponent <IPsiServices>();

                var buildPsiSourceFile = textControl.Document.GetPsiSourceFile(solution).NotNull();
                var buildCSharpFile = psiFiles.GetPsiFiles <CSharpLanguage>(buildPsiSourceFile).OfType <ICSharpFile>().Single();

                var target = new DeclaredElementInstance <ITypeElement>(_taskClass);
                var usingDirective = CSharpElementFactory.GetInstance(buildCSharpFile).CreateUsingStaticDirective(target);

                if (UsingUtil.GetImportConflicts(buildCSharpFile, target).Any())
                {
                    MessageBox.ShowError("Unable to import static class (import introduces conflicts in file)");
                    return;
                }

                using (var cookie = new PsiTransactionCookie(psiServices, DefaultAction.Rollback, typeof(MissingTaskUsageFix).Name))
                {
                    UsingUtil.AddImportTo(buildCSharpFile, usingDirective);
                    cookie.Commit();
                }

                var method = _taskClass.Methods.First(x => x.ShortName.Equals(_error.Reference.GetName(), StringComparison.OrdinalIgnoreCase));
                textControl.Document.ReplaceText(_error.CalculateRange().TextRange, method.ShortName);
            });
        }
Exemplo n.º 2
0
        public static void AddMissingNamespaceImport(ICSharpTypeAndNamespaceHolderDeclaration importScope, CSharpElementFactory factory, string importName)
        {
            var importedNamespace = GetNamespace(factory, importName);

            if (!UsingUtil.CheckAlreadyImported(importScope, importedNamespace))
            {
                UsingUtil.AddImportTo(importScope, importedNamespace);
            }
        }
Exemplo n.º 3
0
        private void RemoveSystemNamespaceUsingIfPossible()
        {
            var usingDirective = _factory.CreateUsingDirective("using $0",
                                                               typeof(ArgumentNullException).Namespace);

            var realUsing = _currentFile.Imports.FirstOrDefault(
                ud => ud.ImportedSymbolName.QualifiedName == usingDirective.ImportedSymbolName.QualifiedName);

            if (realUsing == null)
            {
                return;
            }

            var usages = UsingUtil.GetUsingDirectiveUsage(realUsing);

            if (usages.Count == 0)
            {
                UsingUtil.RemoveImport(realUsing);
            }
        }
Exemplo n.º 4
0
        private static TypeAndNamespace GetAccessableTypeElementAndNamespace(ICSharpTypeMemberDeclaration declaration, ISolution solution, ICSharpFile file, IClrDeclaredElement element, IdentifierLookupScopes scope)
        {
            //IPsiModule module = element.Module;

            IXmlDocIdOwner idOwner = element as IXmlDocIdOwner;
            string docId = idOwner == null ? element.ShortName : idOwner.XMLDocId;

            // Get the defining type.
            ITypeElement typeElement = element as ITypeElement ?? element.GetContainingType();
            if (typeElement == null) return null;

            // Create the result
            TypeAndNamespace result = new TypeAndNamespace
                                          {
                                              XmlDocId = docId,
                                              TypeElement = element
                                          };

            // Get the namespace it belongs to.
            INamespace namespaceElement = typeElement.GetContainingNamespace();
            string namespaceName = namespaceElement.QualifiedName;

            // Check if we're ignoring this namespace
            foreach (string namespacePrefix in NamespacePrefixesToIgnore)
            {
                if (namespaceName.StartsWith(namespacePrefix)) return null;
            }

            // Check if it would be possible to access the type
            AccessRights elementAccessRights = GetAccessRights(element);
            if (elementAccessRights == AccessRights.PRIVATE)
            {
                return null;
            }

            // Check if the type is defined in this solution
            IList<IDeclaration> declarations = element.GetDeclarations();
            if (declarations.Count == 0)
            {
                // Assembly is an import so no internal things allowed
                if (elementAccessRights == AccessRights.INTERNAL) return null;
            }

            // Check if the given namespace is already imported in this file.
            if (UsingUtil.CheckAlreadyImported(file, new DeclaredElementInstance(namespaceElement)) || declaration.GetContainingNamespaceDeclaration().QualifiedName.StartsWith(namespaceName))
            {
                string newDocId = docId[1] == ':' ? docId.Substring(2) : docId;
                if (newDocId.StartsWith(namespaceName + ".")) newDocId = newDocId.Substring(namespaceName.Length + 1);
                result.XmlDocId = newDocId;
                return result;
            }

            // If we require it to be in project or using scope then this is not a match
            if (scope == IdentifierLookupScopes.ProjectAndUsings || scope == IdentifierLookupScopes.ProjectOnly)
            {
                return null;
            }

            // No - so add in the namespace.
            result.RequiredNamespaceImport = namespaceElement;

            return result;
        }