Example #1
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();
 }
Example #2
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;
        }
Example #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();
 }
        private static IEnumerable<SyntaxKind> GetAvailableKeywords(SyntaxTree syntaxTree, SourceLocation position)
        {
            var isInNonUserCode = syntaxTree.Root.InNonUserCode(position);
            if (isInNonUserCode)
                yield break;

            var isPreprocessorDirectiveContext = syntaxTree.DefinitelyInMacro(position);

            var leftToken = syntaxTree.Root.FindTokenOnLeft(position);

            var targetToken = leftToken.GetPreviousTokenIfTouchingWord(position);
            if (targetToken == null)
                yield break;

            var isPreprocessorKeywordContext = isPreprocessorDirectiveContext && syntaxTree.IsPreprocessorKeywordContext(position, leftToken);

            var isStatementContext = !isPreprocessorDirectiveContext && targetToken.IsBeginningOfStatementContext();

            var isSemanticContext = !isPreprocessorDirectiveContext && leftToken.HasAncestor<SemanticSyntax>();

            var isTypeDeclarationContext = syntaxTree.IsTypeDeclarationContext(targetToken);

            if (IsValidBreakKeywordContext(isStatementContext, leftToken))
                yield return SyntaxKind.BreakKeyword;

            if (targetToken.IsSwitchLabelContext())
                yield return SyntaxKind.CaseKeyword;

            if (IsValidContinueKeywordContext(isStatementContext, leftToken))
                yield return SyntaxKind.ContinueKeyword;

            if (isPreprocessorDirectiveContext || IsValidElseKeywordContext(targetToken))
                yield return SyntaxKind.ElseKeyword;

            if (isPreprocessorKeywordContext || isStatementContext)
                yield return SyntaxKind.IfKeyword;

            if (isSemanticContext)
                yield return SyntaxKind.PackoffsetKeyword;

            if (isStatementContext)
                yield return SyntaxKind.ReturnKeyword;

            if (isSemanticContext)
                yield return SyntaxKind.RegisterKeyword;

            if (isTypeDeclarationContext)
                yield return SyntaxKind.StructKeyword;

            if (isStatementContext)
                yield return SyntaxKind.SwitchKeyword;

            if (isStatementContext || IsValidWhileKeywordContext(targetToken))
                yield return SyntaxKind.WhileKeyword;
        }
Example #5
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;
        }
Example #6
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);
        }
        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;
            }
        }
        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();
 }
Example #10
0
 public void ClassifySyntax(SyntaxTree syntaxTree)
 {
     ClassifyNode(syntaxTree.Root);
 }
Example #11
0
 public Compilation(SyntaxTree syntaxTree)
 {
     SyntaxTree = syntaxTree;
 }
Example #12
0
 private static string FormatCode(string sourceCode, SyntaxTree syntaxTree)
 {
     var edits = Formatter.GetEdits(syntaxTree, new TextSpan(syntaxTree.Text, 0, sourceCode.Length), new FormattingOptions());
     return Formatter.ApplyEdits(sourceCode, edits);
 }
Example #13
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));
 }