public IJavaScriptLiteralExpression GetTreeNode()
        {
            var range = TreeTextRange.FromLength(new TreeOffset(DeclarationOffset), ShortName.Length);
            var node  = SourceFile.GetPrimaryPsiFile()?.FindNodeAt(range);

            return(JavaScriptLiteralExpressionNavigator.GetByLiteral(node as ITokenNode));
        }
Exemplo n.º 2
0
        private void HighlightDeclarationsInFile(IDeclaredElement declaredElement, IPsiView psiView, HighlightingsConsumer consumer)
        {
            // There are no IDeclarations for this declared element, try and find the associated string literal expression
            var asmdefNameDeclaredElement = declaredElement as AsmDefNameDeclaredElement;

            if (asmdefNameDeclaredElement == null)
            {
                return;
            }

            foreach (var psiSourceFile in psiView.SortedSourceFiles)
            {
                if (psiSourceFile != asmdefNameDeclaredElement.SourceFile)
                {
                    continue;
                }

                var primaryPsiFile = psiSourceFile.GetPrimaryPsiFile();
                var node           = primaryPsiFile?.FindNodeAt(TreeTextRange.FromLength(
                                                                    new TreeOffset(asmdefNameDeclaredElement.DeclarationOffset),
                                                                    asmdefNameDeclaredElement.ShortName.Length));
                var literalExpression = node?.GetContainingNode <IJavaScriptLiteralExpression>();
                if (literalExpression != null)
                {
                    HighlightFoundDeclaration(literalExpression, consumer);
                }
            }
        }
        public IJsonNewLiteralExpression GetTreeNode()
        {
            var range = TreeTextRange.FromLength(new TreeOffset(DeclarationOffset), ShortName.Length);
            var node  = SourceFile.GetPrimaryPsiFile()?.FindNodeAt(range);

            return(node?.Parent as IJsonNewLiteralExpression);
        }
        private static void MakeDocumentChanges(
            ISolution solution,
            List <FileGeneratorOutput> fileGeneratorOutputs,
            List <InPlaceGeneratorOutput> inPlaceGeneratorOutputs)
        {
            foreach (FileGeneratorOutput output in fileGeneratorOutputs)
            {
                IDocument document = output.GeneratedProjectFile.GetDocument();
                document.ReplaceText(document.DocumentRange, output.GeneratedText);
                ExecutionHelpers.OrginizeUsingsAndFormatFile(solution, document);
            }

            foreach (InPlaceGeneratorOutput output in inPlaceGeneratorOutputs)
            {
                IDocument document = output.ProjectFile.GetDocument();
                if (!output.RangeToDelete.IsEmpty)
                {
                    document.DeleteText(output.RangeToDelete);
                }

                document.InsertText(output.PositionToInsert, output.GeneratedText);

                var treeTextRange = TreeTextRange.FromLength(new TreeOffset(output.PositionToInsert), output.GeneratedText.Length);
                ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, output.MissingUsingDirectives);
            }
        }
 public override void FindNodesAtInternal(TreeTextRange relativeRange, List <ITreeNode> result,
                                          bool includeContainingNodes)
 {
     if (relativeRange.ContainedIn(TreeTextRange.FromLength(GetTextLength())))
     {
         base.FindNodesAtInternal(relativeRange, result, includeContainingNodes);
     }
 }
Exemplo n.º 6
0
        public override IChameleonNode FindChameleonWhichCoversRange(TreeTextRange textRange)
        {
            if (textRange.ContainedIn(TreeTextRange.FromLength(GetTextLength())))
            {
                return(base.FindChameleonWhichCoversRange(textRange) ?? this);
            }

            return(null);
        }
Exemplo n.º 7
0
        public override ITreeNode FindNodeAt(TreeTextRange treeRange)
        {
            if (treeRange.IntersectsOrContacts(TreeTextRange.FromLength(GetTextLength())))
            {
                return(base.FindNodeAt(treeRange));
            }

            return(null);
        }
        public TreeTextRange Translate(DocumentRange documentRange)
        {
            if (!documentRange.IsValid())
            {
                return(TreeTextRange.InvalidRange);
            }
            if (!SourceFile.IsValid())
            {
                return(TreeTextRange.InvalidRange);
            }
            if (!FileLikeNode.IsValid())
            {
                return(TreeTextRange.InvalidRange);
            }

            if (documentRange.Document != SourceFile.Document)
            {
                // That document might appear among the includes
                var rangeFromIncludes = Includes
                                        .Select(include => include.DocumentRangeTranslator.Translate(documentRange))
                                        .Where(textRange => textRange.IsValid())
                                        // Allow FirstOrDefault to return null
                                        .Select <TreeTextRange, TreeTextRange?>(it => it)
                                        .FirstOrDefault();
                return(rangeFromIncludes ?? TreeTextRange.InvalidRange);
            }

            // The range is in the same document as the source file we are responsible for,
            // so we have no choice but to handle the request ourselves
            (int documentStartOffset, int documentEndOffset) = documentRange.TextRange;
            var rootStartOffset = FileLikeNode.GetTreeStartOffset();

            // No includes, tree and document are matching
            if (!Includes.Any())
            {
                return(new TreeTextRange(rootStartOffset + documentStartOffset, rootStartOffset + documentEndOffset));
            }

            var treeStartOffset = Translate(documentStartOffset);

            if (!treeStartOffset.IsValid())
            {
                return(TreeTextRange.InvalidRange);
            }
            return(TreeTextRange.FromLength(treeStartOffset, documentRange.Length));
        }
        public virtual IReference[] GetReferences(ITreeNode element, IReference[] oldReferences)
        {
            if (ResolveUtil.CheckThatAllReferencesBelongToElement <IRazorReference>(oldReferences, element))
            {
                return(oldReferences);
            }
            if (!IsAppropriateNode(element))
            {
                return(EmptyArray <IReference> .Instance);
            }

            IExpression annotatedSectionExpression = razorServices.GetAnnotatedLiteralExpression(element, RazorSectionExternalAttributeName, assignmentChecker);

            if (annotatedSectionExpression is TLiteralExpression && annotatedSectionExpression.ConstantValue.IsString())
            {
                return(new IReference[]
                {
                    new RazorSectionDeclarationReference <TLiteralExpression>(annotatedSectionExpression)
                });
            }

            IExpression annotatedLiteralExpression = razorServices.GetAnnotatedLiteralExpression(element, RazorLayoutExternalAttributeName, assignmentChecker);

            if (annotatedLiteralExpression == null || !annotatedLiteralExpression.ConstantValue.IsString())
            {
                return(EmptyArray <IReference> .Instance);
            }

            IPsiSourceFile sourceFile = element.GetDocumentRange()
                                        .Document.IfNotNull(_ => _.GetPsiSourceFiles(solution),
                                                            EmptyList <IPsiSourceFile> .InstanceList)
                                        .Concat(element.GetSourceFile())
                                        .WhereNotNull()
                                        .FirstOrDefault();

            FileSystemPath location      = sourceFile.GetLocation();
            PathQualifier  pathQualifier = (!location.IsEmpty) ? new PathQualifier(solution, location.Directory) : null;

            return(new IReference[]
            {
                new NancyRazorLayoutReference <ITreeNode>(annotatedLiteralExpression, pathQualifier,
                                                          annotatedLiteralExpression,
                                                          TreeTextRange.FromLength(annotatedLiteralExpression.GetTextLength()),
                                                          sourceFile.IfNotNull(_ => _.LanguageType), true, true)
            });
        }
        public IList <IDeclaration> GetDeclarations()
        {
            if (!(mySourceFile.GetPrimaryPsiFile() is IShaderLabFile psi))
            {
                return(EmptyList <IDeclaration> .InstanceList);
            }

            var node = psi.FindNodeAt(TreeTextRange.FromLength(new TreeOffset(TreeOffset), 1));

            while (node != null && !(node is IDeclaration))
            {
                node = node.Parent;
            }
            if (node == null)
            {
                return(EmptyList <IDeclaration> .Instance);
            }

            return(new[] { (IDeclaration)node });
        }
        protected override bool TryExecute(IInPlaceGenerator generator, GeneratorExecutionHost executionHost)
        {
            IDocument document = generator.DataProvider.Document;

            bool executed = ExecutionHelpers.TryExecuteGenerator(
                generator,
                out string generatedText,
                out TextRange rangeToDelete,
                out int positionToInsert,
                out IReadOnlyCollection <IUsingDirective> missingUsingDirectives
                );

            if (!executed)
            {
                return(false);
            }

            ISolution solution = generator.DataProvider.Solution;

            using (var progressIndicator = NullProgressIndicator.Create())
            {
                IProjectModelTransactionCookie transaction =
                    solution.CreateTransactionCookie(DefaultAction.Rollback, "T4 Generating file", progressIndicator);
                using (transaction)
                {
                    if (!rangeToDelete.IsEmpty)
                    {
                        document.DeleteText(rangeToDelete);
                    }

                    document.InsertText(positionToInsert, generatedText);

                    var treeTextRange = TreeTextRange.FromLength(new TreeOffset(positionToInsert), generatedText.Length);
                    ExecutionHelpers.FormatFileRangeAndAddUsingDirectives(solution, document, treeTextRange, missingUsingDirectives);

                    transaction.Commit(progressIndicator);
                }
            }

            return(true);
        }
Exemplo n.º 12
0
 public override IChameleonNode FindChameleonWhichCoversRange(TreeTextRange textRange)
 {
     return(textRange.ContainedIn(TreeTextRange.FromLength(this.GetTextLength()))
                ? this
                : null);
 }
 public override ITreeNode FindNodeAt(TreeTextRange treeRange) =>
 treeRange.IntersectsOrContacts(TreeTextRange.FromLength(GetTextLength()))
 ? base.FindNodeAt(treeRange)
 : null;