Esempio n. 1
0
        public static List <Run> ToRuns(this SyntaxToken token, IClassificationFormatMap formatMap, IClassificationTypeRegistryService typeRegistry)
        {
            List <Run> runs = new List <Run>();

            if (token.HasLeadingTrivia)
            {
                runs.Add(token.LeadingTrivia.GetTextAndReplaceNewLines(string.Empty).ToRun(formatMap, typeRegistry.GetClassificationType(PredefinedClassificationTypeNames.WhiteSpace)));
            }

            runs.Add(token.Text.ToRun(formatMap, typeRegistry.GetClassificationType(token.GetClassificationName())));

            if (token.HasTrailingTrivia)
            {
                runs.Add(token.TrailingTrivia.GetTextAndReplaceNewLines(" ").ToRun(formatMap, typeRegistry.GetClassificationType(PredefinedClassificationTypeNames.WhiteSpace)));
            }

            return(runs);
        }
Esempio n. 2
0
        public IList <ClassificationSpan> GetClassificationSpans(SnapshotSpan span)
        {
            VSSnapshot currentSnapshot = new VSSnapshot(this.source, span.Snapshot);

            LinkedList <Token>        tokens = this.lexer.Run(currentSnapshot, span.Start.GetContainingLine().ExtentIncludingLineBreak.Span.ToGLSLSpan());
            List <ClassificationSpan> spans  = new List <ClassificationSpan>();

            SyntaxTree tree = this.source.Tree;

            if (tree == null)
            {
                return(spans);
            }

            foreach (Token token in tokens)
            {
                if (!span.OverlapsWith(token.Span.ToVSSpan()))
                {
                    continue;
                }

                SyntaxToken syntaxToken = tree?.GetNodeFromPosition(this.source.CurrentSnapshot, token.Span.Start) as SyntaxToken;

                string classificationName = string.Empty;

                if (syntaxToken != null)
                {
                    classificationName = syntaxToken.GetClassificationName();
                }
                else
                {
                    if (token.SyntaxType.IsPreprocessor())
                    {
                        classificationName = GLSLConstants.PreprocessorKeyword;
                    }
                    else if (token.SyntaxType.IsPunctuation())
                    {
                        classificationName = GLSLConstants.Punctuation;
                    }
                    else if (token.SyntaxType.IsKeyword())
                    {
                        classificationName = GLSLConstants.Keyword;
                    }
                    else if (token.SyntaxType.IsNumber())
                    {
                        classificationName = GLSLConstants.Number;
                    }
                    else if (token.SyntaxType == SyntaxType.IdentifierToken)
                    {
                        classificationName = GLSLConstants.Identifier;
                    }
                }

                if (!string.IsNullOrEmpty(classificationName))
                {
                    SnapshotSpan snapshotSpan = new SnapshotSpan(span.Snapshot, token.Span.ToVSSpan());

                    spans.Add(new ClassificationSpan(snapshotSpan, this.classificationTypeRegistryService.GetClassificationType(classificationName)));
                }
            }

            this.ColorComments(span, spans, currentSnapshot, this.lexer.CommentSpans);

            return(spans);
        }