예제 #1
0
        public static IList<Edit> GetEdits(SyntaxTree syntaxTree, TextSpan textSpan, FormattingOptions options)
        {
            // Format.
            var formattingVisitor = new FormattingVisitor(syntaxTree, textSpan, options);
            formattingVisitor.Visit(syntaxTree.Root);

            return formattingVisitor.Edits.Values;
        }
예제 #2
0
 private static bool IsInSemantic(SyntaxTree syntaxTree, SourceLocation position)
 {
     var token = syntaxTree.Root.FindTokenOnLeft(position);
     return token.Parent
         .AncestorsAndSelf()
         .OfType<SemanticSyntax>()
         .Any();
 }
예제 #3
0
 private static BraceMatchingResult MatchBraces(SyntaxTree syntaxTree, SourceLocation position, SyntaxKind leftKind, SyntaxKind rightKind)
 {
     return syntaxTree.Root.FindStartTokens(position)
         .Select(t => MatchBraces(t, position, leftKind, rightKind))
         .Where(r => r.IsValid)
         .DefaultIfEmpty(BraceMatchingResult.None)
         .First();
 }
예제 #4
0
 public BraceMatchingResult MatchBraces(SyntaxTree syntaxTree, SourceLocation position)
 {
     return _matchingKinds
         .Select(k => MatchBraces(syntaxTree, position, k.Item1, k.Item2))
         .Where(r => r.IsValid)
         .DefaultIfEmpty(BraceMatchingResult.None)
         .First();
 }
예제 #5
0
        private static IEnumerable<SyntaxKind> GetAvailableKeywords(SyntaxTree syntaxTree, SourceLocation position)
        {
            if (IsInSemantic(syntaxTree, position))
            {
                yield return SyntaxKind.PackoffsetKeyword;
                yield return SyntaxKind.RegisterKeyword;
            }

            yield return SyntaxKind.TrueKeyword;
            yield return SyntaxKind.FalseKeyword;
        }
예제 #6
0
        private static SyntaxTree Parse(SourceText sourceText, ParserOptions options, IIncludeFileSystem fileSystem, Func<HlslParser, SyntaxNode> parseFunc)
        {
            var lexer = new HlslLexer(sourceText, options, fileSystem);
            var parser = new HlslParser(lexer);

            var result = new SyntaxTree(sourceText,
                syntaxTree => new Tuple<SyntaxNode, List<FileSegment>>(
                    parseFunc(parser),
                    lexer.FileSegments));

            Debug.WriteLine(DateTime.Now +  " - Finished parsing");

            return result;
        }
예제 #7
0
        public static IList<Edit> GetEditsAfterKeystroke(SyntaxTree syntaxTree, int position, char character, FormattingOptions options)
        {
            var location = syntaxTree.MapRootFilePosition(position);

            // Find span of block / statement terminated by the character just typed.
            var token = syntaxTree.Root.FindTokenOnLeft(location);
            if (token.Text != character.ToString())
                return new List<Edit>();

            // Get span of node containing this token.
            var span = token.Parent.GetTextSpanRoot();
            if (span == TextSpan.None)
                return new List<Edit>();

            return GetEdits(syntaxTree, span, options);
        }
예제 #8
0
        private static bool TryGetSyntaxTree(ITextBuffer textBuffer, out SyntaxTree syntaxTree)
        {
            try
            {
                var syntaxTreeTask = Task.Run(() => textBuffer.CurrentSnapshot.GetSyntaxTree(CancellationToken.None));

                if (!syntaxTreeTask.Wait(TimeSpan.FromSeconds(5)))
                {
                    Logger.Log("Parsing timeout");
                    syntaxTree = null;
                    return false;
                }

                syntaxTree = syntaxTreeTask.Result;
                return true;
            }
            catch (Exception ex)
            {
                Logger.Log("Parsing error: " + ex);
                syntaxTree = null;
                return false;
            }
        }
예제 #9
0
 public SnapshotSyntaxTree(ITextSnapshot snapshot, SyntaxTree syntaxTree)
 {
     Snapshot = snapshot;
     SyntaxTree = syntaxTree;
 }
예제 #10
0
 private static string FormatCode(string sourceCode, SyntaxTree syntaxTree)
 {
     var edits = Formatter.GetEdits(syntaxTree, new TextSpan(null, 0, sourceCode.Length), new FormattingOptions());
     return Formatter.ApplyEdits(sourceCode, edits);
 }
        private async void RefreshSyntaxVisualizer()
        {
            if (!IsVisible || _activeWpfTextView == null)
                return;

            var currentSnapshot = _activeWpfTextView.TextBuffer.CurrentSnapshot;
            var contentType = currentSnapshot.ContentType;
            if (!contentType.IsOfType(HlslConstants.ContentTypeName))
                return;

            try
            {
                _activeSyntaxTree = await Task.Run(() => currentSnapshot.GetSyntaxTree(CancellationToken.None));
            }
            catch (Exception ex)
            {
                Logger.Log("Failed to get syntax tree for syntax visualizer: " + ex);
                return;
            }
            
            DisplaySyntaxTree();
            NavigateFromSource();
        }
 private void Clear()
 {
     if (_typingTimer != null)
     {
         _typingTimer.Stop();
         _typingTimer.Tick -= HandleTypingTimerTimeout;
         _typingTimer = null;
     }
     if (_activeWpfTextView != null)
     {
         _activeWpfTextView.Selection.SelectionChanged -= HandleSelectionChanged;
         _activeWpfTextView.TextBuffer.Changed -= HandleTextBufferChanged;
         _activeWpfTextView.LostAggregateFocus -= HandleTextViewLostFocus;
         _activeWpfTextView = null;
     }
     _activeSyntaxTree = null;
     TreeView.Items.Clear();
 }
예제 #13
0
 public Compilation(SyntaxTree syntaxTree)
 {
     SyntaxTree = syntaxTree;
 }
 private static IEnumerable<SyntaxKind> GetAvailableKeywords(SyntaxTree syntaxTree, SourceLocation position)
 {
     yield return SyntaxKind.TrueKeyword;
     yield return SyntaxKind.FalseKeyword;
 }
예제 #15
0
 public void ClassifySyntax(SyntaxTree syntaxTree)
 {
     ClassifyNode(syntaxTree.Root);
 }
예제 #16
0
 public static void CheckForParseErrors(SyntaxTree syntaxTree)
 {
     foreach (var diagnostic in syntaxTree.GetDiagnostics())
         Debug.WriteLine(diagnostic.ToString());
     Assert.That(syntaxTree.GetDiagnostics().Count(), Is.EqualTo(0));
 }