public void AddOrReplace(int key, TriviaData triviaInfo) { // PERF: Set the concurrency level to 1 because, while the dictionary has to be thread-safe, // there is very little contention in formatting. A lower concurrency level reduces object // allocations which are used internally by ConcurrentDictionary for locking. var map = LazyInitialization.EnsureInitialized(ref _map, () => new ConcurrentDictionary<int, TriviaData>(concurrencyLevel: 1, capacity: 8)); map[key] = triviaInfo; }
public void AddOrReplace(int key, TriviaData triviaInfo) { // PERF: Set the concurrency level to 1 because, while the dictionary has to be thread-safe, // there is very little contention in formatting. A lower concurrency level reduces object // allocations which are used internally by ConcurrentDictionary for locking. var map = LazyInitialization.EnsureInitialized(ref _map, () => new ConcurrentDictionary <int, TriviaData>(concurrencyLevel: 1, capacity: 8)); map[key] = triviaInfo; }
public void AddOrReplace(int key, TriviaData triviaInfo) { if (this.Contains(key)) { Replace(key, triviaInfo); return; } Add(key, triviaInfo); }
private void AddTextChanges(List<TextChange> list, SyntaxToken token1, SyntaxToken token2, TriviaData data) { var span = TextSpan.FromBounds(token1.RawKind == 0 ? this.TreeInfo.StartPosition : token1.Span.End, token2.RawKind == 0 ? this.TreeInfo.EndPosition : token2.SpanStart); var originalString = this.TreeInfo.GetTextBetween(token1, token2); foreach (var change in data.GetTextChanges(span)) { var oldText = (change.Span == span) ? originalString : originalString.Substring(change.Span.Start - span.Start, change.Span.Length); list.Add(change.SimpleDiff(oldText)); } }
public void ApplyChange(int pairIndex, TriviaData data) { Contract.ThrowIfNull(data); Contract.ThrowIfFalse(0 <= pairIndex && pairIndex < this.TokenCount - 1); // do reference equality check var sameAsOriginal = GetOriginalTriviaData(pairIndex) == data; if (sameAsOriginal) { _changes.TryRemove(pairIndex); } else { _changes.AddOrReplace(pairIndex, data); } }
private void ApplyIndentationToGivenPosition( TokenData previousToken, TokenData currentToken, TriviaData triviaInfo, int baseSpaceOrIndentation, Dictionary <SyntaxToken, int> previousChangesMap, CancellationToken cancellationToken ) { // add or replace existing value. this could happen if a token get moved multiple times // due to one being involved in multiple alignment operations previousChangesMap[currentToken.Token] = triviaInfo.Spaces; if (previousToken.IndexInStream < 0 || triviaInfo.Spaces == baseSpaceOrIndentation) { return; } // before make any change, check whether spacing is allowed var spanBetweenTokens = TextSpan.FromBounds( previousToken.Token.Span.End, currentToken.Token.SpanStart ); if (_context.IsSpacingSuppressed(spanBetweenTokens, triviaInfo.TreatAsElastic)) { return; } if (_context.IsFormattingDisabled(spanBetweenTokens)) { return; } // okay, update indentation _context.TokenStream.ApplyChange( previousToken.IndexInStream, triviaInfo.WithIndentation( baseSpaceOrIndentation, _context, _formattingRules, cancellationToken ) ); }
private void ApplyIndentationDelta( int pairIndex, SyntaxToken currentToken, int indentationDelta, TriviaData triviaInfo, Dictionary <SyntaxToken, int> previousChangesMap, CancellationToken cancellationToken ) { Contract.ThrowIfFalse(triviaInfo.SecondTokenIsFirstTokenOnLine); var indentation = triviaInfo.Spaces + indentationDelta; if (triviaInfo.Spaces == indentation) { // indentation didn't actually move. nothing to change return; } Debug.Assert(!_context.IsFormattingDisabled(pairIndex)); // record the fact that this pair has been moved Debug.Assert(!previousChangesMap.ContainsKey(currentToken)); previousChangesMap.Add(currentToken, triviaInfo.Spaces); // okay, update indentation _context.TokenStream.ApplyChange( pairIndex, triviaInfo.WithIndentation( indentation, _context, _formattingRules, cancellationToken ) ); }
private void ApplyIndentationToGivenPosition( TokenData previousToken, TokenData currentToken, TriviaData triviaInfo, int baseSpaceOrIndentation, Dictionary<SyntaxToken, int> previousChangesMap, CancellationToken cancellationToken) { // add or replace existing value. this could happen if a token get moved multiple times // due to one being involved in multiple alignment operations previousChangesMap[currentToken.Token] = triviaInfo.Spaces; if (previousToken.IndexInStream < 0 || triviaInfo.Spaces == baseSpaceOrIndentation) { return; } // before make any change, check whether spacing is allowed var spanBetweenTokens = TextSpan.FromBounds(previousToken.Token.Span.End, currentToken.Token.SpanStart); if (this.context.IsSpacingSuppressed(spanBetweenTokens)) { return; } // okay, update indentation this.tokenStream.ApplyChange( previousToken.IndexInStream, triviaInfo.WithIndentation(baseSpaceOrIndentation, context, formattingRules, cancellationToken)); }
public void Replace(int key, TriviaData triviaInfo) { Contract.ThrowIfFalse(this.Contains(key)); _map[key] = triviaInfo; }
public void Add(int key, TriviaData triviaInfo) { Contract.ThrowIfTrue(this.Contains(key)); _map.TryAdd(key, triviaInfo); }
public void ApplyChange(int pairIndex, TriviaData data) { Contract.ThrowIfNull(data); Contract.ThrowIfFalse(0 <= pairIndex && pairIndex < this.TokenCount - 1); // do reference equality check var sameAsOriginal = GetOriginalTriviaData(pairIndex) == data; if (this.changes.Contains(pairIndex)) { if (sameAsOriginal) { this.changes.Remove(pairIndex); return; } // okay it already exist. // replace existing one this.changes.Replace(pairIndex, data); return; } // triviaInfo is same as original, nothing to do here. if (sameAsOriginal) { return; } this.changes.Add(pairIndex, data); }
// local functions static void BeginningOfTreeTriviaInfoApplier(int i, TokenStream ts, TriviaData info) => ts.ApplyBeginningOfTreeChange(info);
private void AddTextChanges(List <TextChange> list, SyntaxToken token1, SyntaxToken token2, TriviaData data) { var span = TextSpan.FromBounds(token1.RawKind == 0 ? this.TreeInfo.StartPosition : token1.Span.End, token2.RawKind == 0 ? this.TreeInfo.EndPosition : token2.SpanStart); var originalString = this.TreeInfo.GetTextBetween(token1, token2); foreach (var change in data.GetTextChanges(span)) { var oldText = (change.Span == span) ? originalString : originalString.Substring(change.Span.Start - span.Start, change.Span.Length); list.Add(change.SimpleDiff(oldText)); } }
public void Add(int key, TriviaData triviaInfo) { Contract.ThrowIfTrue(this.Contains(key)); this.map.TryAdd(key, triviaInfo); }
public void Replace(int key, TriviaData triviaInfo) { Contract.ThrowIfFalse(this.Contains(key)); this.map[key] = triviaInfo; }
private SyntaxTriviaList GetLeadingTriviaAtBeginningOfTree( ValueTuple<SyntaxToken, SyntaxToken> pair, TriviaData triviaData, CancellationToken cancellationToken) { var csharpTriviaData = triviaData as TriviaDataWithList; if (csharpTriviaData != null) { return SyntaxFactory.TriviaList(csharpTriviaData.GetTriviaList(cancellationToken)); } // whitespace trivia case such as spaces/tabs/new lines // these will always have single text changes var text = triviaData.GetTextChanges(GetTextSpan(pair)).Single().NewText; return SyntaxFactory.ParseLeadingTrivia(text); }
private void ApplyIndentationDelta( int pairIndex, SyntaxToken currentToken, int indentationDelta, TriviaData triviaInfo, Dictionary<SyntaxToken, int> previousChangesMap, CancellationToken cancellationToken) { Contract.ThrowIfFalse(triviaInfo.SecondTokenIsFirstTokenOnLine); var indentation = triviaInfo.Spaces + indentationDelta; if (triviaInfo.Spaces == indentation) { // indentation didn't actually move. nothing to change return; } // record the fact that this pair has been moved Debug.Assert(!previousChangesMap.ContainsKey(currentToken)); previousChangesMap.Add(currentToken, triviaInfo.Spaces); // okay, update indentation this.tokenStream.ApplyChange(pairIndex, triviaInfo.WithIndentation(indentation, context, formattingRules, cancellationToken)); }
public bool TryGet(int key, out TriviaData triviaInfo) { triviaInfo = null; return _map?.TryGetValue(key, out triviaInfo) ?? false; }
public void ApplyBeginningOfTreeChange(TriviaData data) { Contract.ThrowIfNull(data); _changes.AddOrReplace(Changes.BeginningOfTreeKey, data); }
public bool TryGet(int key, out TriviaData triviaInfo) { triviaInfo = null; return(_map?.TryGetValue(key, out triviaInfo) ?? false); }
public void ApplyEndOfTreeChange(TriviaData data) { Contract.ThrowIfNull(data); _changes.AddOrReplace(Changes.EndOfTreeKey, data); }
public void ApplyEndOfTreeChange(TriviaData data) { Contract.ThrowIfNull(data); this.changes.AddOrReplace(Changes.EndOfTreeKey, data); }
public void ApplyBeginningOfTreeChange(TriviaData data) { Contract.ThrowIfNull(data); this.changes.AddOrReplace(Changes.BeginningOfTreeKey, data); }