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; }
private static bool IsInSemantic(SyntaxTree syntaxTree, SourceLocation position) { var token = syntaxTree.Root.FindTokenOnLeft(position); return token.Parent .AncestorsAndSelf() .OfType<SemanticSyntax>() .Any(); }
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(); }
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(); }
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; }
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; }
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; } }
public SnapshotSyntaxTree(ITextSnapshot snapshot, SyntaxTree syntaxTree) { Snapshot = snapshot; SyntaxTree = syntaxTree; }
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(); }
public Compilation(SyntaxTree syntaxTree) { SyntaxTree = syntaxTree; }
private static IEnumerable<SyntaxKind> GetAvailableKeywords(SyntaxTree syntaxTree, SourceLocation position) { yield return SyntaxKind.TrueKeyword; yield return SyntaxKind.FalseKeyword; }
public void ClassifySyntax(SyntaxTree syntaxTree) { ClassifyNode(syntaxTree.Root); }
public static void CheckForParseErrors(SyntaxTree syntaxTree) { foreach (var diagnostic in syntaxTree.GetDiagnostics()) Debug.WriteLine(diagnostic.ToString()); Assert.That(syntaxTree.GetDiagnostics().Count(), Is.EqualTo(0)); }