private static void CloseRegion(List <Region> newRegions, ref PartialRegion currentRegion, ref bool comment_started, ITextSnapshotLine line, string linetext, string LabelName_, bool closing_comment = false) { int currentLevel = (currentRegion != null) ? currentRegion.Level : 1; var currRegionStart = (currentRegion == null) ? -1 : linetext.IndexOf(currentRegion.end_text, StringComparison.Ordinal); if (currRegionStart > -1) { int closingLevel = currentLevel; //! -1 comment_started = false; if (currentRegion != null && currentLevel == closingLevel) { if ((line.LineNumber - currentRegion.StartLine) > 1) //check if comment is only one line { newRegions.Add(new Region { Level = currentLevel, StartLine = currentRegion.StartLine, StartOffset = currentRegion.StartOffset, EndLine = line.LineNumber - (closing_comment ? 1 : 0), ellipsis = currentRegion.ellipsis, LabelName = currentRegion.LabelName, collapsed = currentRegion.collapsed }); var tt = newRegions; } currentRegion = currentRegion.PartialParent; } } }
void CreateOutlineRegions() { //IList<SLang.DECLARATION> decls = ASTUtilities.GetUnitsAndStandalones(_buffer); ITextSnapshot newSnapshot = _buffer.CurrentSnapshot; List <Region> newRegions = new List <Region>(); //keep the current (deepest) partial region, which will have // references to any parent partial regions. PartialRegion currentRegion = null; foreach (var outlineTokenSpan in _outlineTokens ?? Enumerable.Empty <ITagSpan <SLangTokenTag> >()) { var outlineToken = outlineTokenSpan.Tag.token; // Handle opening token if (Constants.outlineStartTokenTypes.Contains(outlineToken.code)) { int currentLevel = (currentRegion != null) ? currentRegion.Level : 1; int newLevel = currentLevel + 1; currentRegion = new PartialRegion() { Level = newLevel, StartLine = outlineToken.span.end.line - 1, StartOffset = outlineToken.span.end.pos - 2, PartialParent = currentRegion }; } // Handle closing token else if (Constants.outlineEndTokenTypes.Contains(outlineToken.code)) { int currentLevel = (currentRegion != null) ? currentRegion.Level : 1; int closingLevel = currentLevel; if (currentRegion != null) { newRegions.Add(new Region() { Level = currentLevel, StartLine = currentRegion.StartLine, StartOffset = currentRegion.StartOffset, EndLine = outlineToken.span.end.line - 1 }); currentRegion = currentRegion.PartialParent; } } } _regions = newRegions; }
private static PartialRegion OpenRegion(List <Region> newRegions, PartialRegion currentRegion, ITextSnapshotLine line, int regionStart, string ellipsis_, string end_text_, string LabelName_, bool collapsed_) { int currentLevel = (currentRegion != null) ? currentRegion.Level : 1; int newLevel = currentLevel + 1; if (currentLevel == newLevel && currentRegion != null) { newRegions.Add(new Region { Level = currentRegion.Level, StartLine = currentRegion.StartLine, StartOffset = currentRegion.StartOffset, EndLine = line.LineNumber, ellipsis = ellipsis_, LabelName = LabelName_, end_text = end_text_, collapsed = collapsed_ }); currentRegion = new PartialRegion { Level = newLevel, StartLine = line.LineNumber, StartOffset = regionStart, PartialParent = currentRegion.PartialParent, ellipsis = ellipsis_, LabelName = LabelName_, end_text = end_text_, collapsed = collapsed_ }; } else { currentRegion = new PartialRegion { Level = newLevel, StartLine = line.LineNumber, StartOffset = regionStart, PartialParent = currentRegion, ellipsis = ellipsis_, LabelName = LabelName_, end_text = end_text_, collapsed = collapsed_ }; } return(currentRegion); }
void ReParse() { ITextSnapshot newSnapshot = buffer.CurrentSnapshot; List <Region> newRegions = new List <Region>(); //keep the current (deepest) partial region, which will have // references to any parent partial regions. PartialRegion currentRegion = null; foreach (var line in newSnapshot.Lines) { int regionStart = -1; string text = line.GetText(); //lines that contain a "[" denote the start of a new region. if ((regionStart = text.IndexOf(startHide, StringComparison.Ordinal)) != -1 || (regionStart = text.IndexOf(startHide.Replace(" ", string.Empty), StringComparison.Ordinal)) != -1) { int currentLevel = (currentRegion != null) ? currentRegion.Level : 1; int newLevel; if (!TryGetLevel(text, regionStart, out newLevel)) { newLevel = currentLevel + 1; } //levels are the same and we have an existing region; //end the current region and start the next if (currentLevel == newLevel && currentRegion != null) { newRegions.Add(new Region() { Level = currentRegion.Level, StartLine = currentRegion.StartLine, StartOffset = currentRegion.StartOffset, EndLine = line.LineNumber }); currentRegion = new PartialRegion() { Level = newLevel, StartLine = line.LineNumber, StartOffset = regionStart, PartialParent = currentRegion.PartialParent }; } //this is a new (sub)region else { currentRegion = new PartialRegion() { Level = newLevel, StartLine = line.LineNumber, StartOffset = regionStart, PartialParent = currentRegion }; } } //lines that contain "]" denote the end of a region else if ((regionStart = text.IndexOf(endHide, StringComparison.Ordinal)) != -1 || (regionStart = text.IndexOf(endHide.Replace(" ", string.Empty), StringComparison.Ordinal)) != -1) { int currentLevel = (currentRegion != null) ? currentRegion.Level : 1; int closingLevel; if (!TryGetLevel(text, regionStart, out closingLevel)) { closingLevel = currentLevel; } //the regions match if (currentRegion != null && currentLevel == closingLevel) { newRegions.Add(new Region() { Level = currentLevel, StartLine = currentRegion.StartLine, StartOffset = currentRegion.StartOffset, EndLine = line.LineNumber }); currentRegion = currentRegion.PartialParent; } } } //determine the changed span, and send a changed event with the new spans List <Span> oldSpans = new List <Span>(this.regions.Select(r => AsSnapshotSpan(r, this.snapshot) .TranslateTo(newSnapshot, SpanTrackingMode.EdgeExclusive) .Span)); List <Span> newSpans = new List <Span>(newRegions.Select(r => AsSnapshotSpan(r, newSnapshot).Span)); NormalizedSpanCollection oldSpanCollection = new NormalizedSpanCollection(oldSpans); NormalizedSpanCollection newSpanCollection = new NormalizedSpanCollection(newSpans); //the changed regions are regions that appear in one set or the other, but not both. NormalizedSpanCollection removed = NormalizedSpanCollection.Difference(oldSpanCollection, newSpanCollection); int changeStart = int.MaxValue; int changeEnd = -1; if (removed.Count > 0) { changeStart = removed[0].Start; changeEnd = removed[removed.Count - 1].End; } if (newSpans.Count > 0) { changeStart = Math.Min(changeStart, newSpans[0].Start); changeEnd = Math.Max(changeEnd, newSpans[newSpans.Count - 1].End); } this.snapshot = newSnapshot; this.regions = newRegions; if (changeStart <= changeEnd) { if (this.TagsChanged != null) { this.TagsChanged(this, new SnapshotSpanEventArgs( new SnapshotSpan(this.snapshot, Span.FromBounds(changeStart, changeEnd)))); } } }
void ReParse() { ITextSnapshot newSnapshot = _buffer.CurrentSnapshot; List <Region> newRegions = new List <Region>(); //keep the current (deepest) partial region, which will have // references to any parent partial _regions. PartialRegion currentRegion = null; bool comment_started = false; foreach (var line in newSnapshot.Lines) { int regionStart = -1; string linetext = line.GetText(); string ellipsis_ = ""; string end_text_ = ""; string LabelName_ = ""; bool collapsed_ = false; string current_line = linetext.Replace('\t', ' '); current_line = current_line.TrimStart(); bool regionFinished_comment = false; #region comment section int comment_start = current_line.IndexOf("//", StringComparison.Ordinal); if (comment_start == 0 && !comment_started) { regionStart = comment_start; ellipsis_ = linetext.Substring(comment_start + 2, linetext.Length - comment_start - 2).Trim(); comment_started = true; if (ellipsis_ == "") { ellipsis_ = "..."; } } if (comment_start != 0 && comment_started) { regionFinished_comment = true; } #endregion #region labels bool regionFinished_label = false; var label_values = current_line.Split(' '); if (label_values.Length >= 2) { bool labelFound = Dictionary_asm.Labels.Contains(label_values[0]); if (labelFound && label_values[0] != "global" && label_values[0] != "local") { if (label_values[0] != "end") { regionStart = current_line.IndexOf(label_values[0], StringComparison.Ordinal); ellipsis_ = label_values[1].Replace('\"', ' ').Trim(); LabelName_ = ellipsis_; } else { regionFinished_label = true; LabelName_ = label_values[1].Replace('\"', ' ').Trim(); } } } #endregion #region closing the region if (regionFinished_comment || regionFinished_label) { CloseRegion(newRegions, ref currentRegion, ref comment_started, line, linetext, LabelName_, regionFinished_comment); if (regionFinished_comment && regionFinished_label) { CloseRegion(newRegions, ref currentRegion, ref comment_started, line, linetext, LabelName_); } } #endregion #region opening the new region if (regionStart > -1) { currentRegion = OpenRegion(newRegions, currentRegion, line, regionStart, ellipsis_, end_text_, LabelName_, collapsed_); } #endregion } if (comment_started) { CloseRegion(newRegions, ref currentRegion, ref comment_started, newSnapshot.Lines.Last(), newSnapshot.Lines.Last().GetText(), null); } //determine the changed span, and send a changed event with the new spans List <Span> oldSpans = new List <Span>(_regions.Select(r => AsSnapshotSpan(r, _snapshot) .TranslateTo(newSnapshot, SpanTrackingMode.EdgeExclusive) .Span)); List <Span> newSpans = new List <Span>(newRegions.Select(r => AsSnapshotSpan(r, newSnapshot).Span)); NormalizedSpanCollection oldSpanCollection = new NormalizedSpanCollection(oldSpans); NormalizedSpanCollection newSpanCollection = new NormalizedSpanCollection(newSpans); //the changed _regions are _regions that appear in one set or the other, but not both. NormalizedSpanCollection removed = NormalizedSpanCollection.Difference(oldSpanCollection, newSpanCollection); int changeStart = int.MaxValue; int changeEnd = -1; if (removed.Count > 0) { changeStart = removed[0].Start; changeEnd = removed[removed.Count - 1].End; } if (newSpans.Count > 0) { changeStart = Math.Min(changeStart, newSpans[0].Start); changeEnd = Math.Max(changeEnd, newSpans[newSpans.Count - 1].End); } _snapshot = newSnapshot; _regions = newRegions; if (changeStart <= changeEnd) { if (TagsChanged != null) { TagsChanged(this, new SnapshotSpanEventArgs( new SnapshotSpan(_snapshot, Span.FromBounds(changeStart, changeEnd)))); } } }
private void Reparse() { ITextSnapshot newSnapshot = this.buffer.CurrentSnapshot; List <Region> newRegions = new List <Region>(); // Keep the current (deepest) partial region, which will have references to any parent partial regions. PartialRegion currentRegion = null; foreach (var line in newSnapshot.Lines) { string text = line.GetText(); // Lines that match a start regex denote the start of a new region. Match startMatch = this.startExpressions.Select(regex => regex.Match(text)).FirstOrDefault(m => m.Success); if (startMatch != null) { int matchStartIndex = startMatch.Index; int currentLevel = (currentRegion != null) ? currentRegion.Level : 1; int newLevel = currentLevel + 1; // Levels are the same, and we have an existing region; // End the current region and start the next. if (currentLevel == newLevel && currentRegion != null) { newRegions.Add(new Region() { Level = currentRegion.Level, StartLine = currentRegion.StartLine, StartOffset = currentRegion.StartOffset, EndLine = line.LineNumber }); currentRegion = new PartialRegion() { Level = newLevel, StartLine = line.LineNumber, StartOffset = matchStartIndex, PartialParent = currentRegion.PartialParent }; } else { // This is a new (sub)region currentRegion = new PartialRegion() { Level = newLevel, StartLine = line.LineNumber, StartOffset = matchStartIndex, PartialParent = currentRegion }; } } else { // Lines that match an end regex denote the end of a region Match endMatch = this.endExpressions.Select(regex => regex.Match(text)).FirstOrDefault(m => m.Success); if (endMatch != null) { int currentLevel = (currentRegion != null) ? currentRegion.Level : 1; int closingLevel = currentLevel; // The regions match if (currentRegion != null && currentLevel == closingLevel) { newRegions.Add(new Region() { Level = currentLevel, StartLine = currentRegion.StartLine, StartOffset = currentRegion.StartOffset, EndLine = line.LineNumber }); currentRegion = currentRegion.PartialParent; } } } } SnapshotRegions newSnapshotRegions = new SnapshotRegions(newSnapshot, newRegions); SnapshotRegions oldSnapshotRegions; lock (this.resourceLock) { oldSnapshotRegions = this.snapshotRegions; this.snapshotRegions = newSnapshotRegions; } this.CheckIfTagsChanged(oldSnapshotRegions, newSnapshotRegions); }
private void Parse() { ITextSnapshot newSnapshot = buffer.CurrentSnapshot; List <Region> newRegions = new List <Region>(); PartialRegion currentRegion = null; int lineOfPreviousToken = -1; foreach (var line in newSnapshot.Lines) { int curLoc = line.Start.Position; foreach (TokenLocation tokenLocation in new TokenScanner(line.GetText(), curLoc, t => t.Token == BabelParser.Tokens.LEX_WHITE)) { if (_startRegionTokens.Contains(tokenLocation.Token)) { if (currentRegion == null) { // Init currentRegion for the first time currentRegion = new PartialRegion() { StartToken = tokenLocation.Token, StartLine = line.LineNumber, }; } else { // Save currentRegion in newRegions, refresh currentRegion newRegions.Add(new Region() { StartToken = currentRegion.StartToken, StartLine = currentRegion.StartLine, TableName = currentRegion.TableName, MeasureName = currentRegion.MeasureName, EndLine = lineOfPreviousToken }); currentRegion = new PartialRegion() { StartToken = tokenLocation.Token, StartLine = line.LineNumber, }; } } // Get table name and measure name for hover text if (currentRegion != null) { if (tokenLocation.Token == BabelParser.Tokens.ESCAPEDTABLENAME || tokenLocation.Token == BabelParser.Tokens.TABLENAME && currentRegion.TableName == null) { currentRegion.TableName = newSnapshot.GetText(tokenLocation.Location, tokenLocation.Length); } if (tokenLocation.Token == BabelParser.Tokens.COLUMNNAME && currentRegion.MeasureName == null) { currentRegion.MeasureName = newSnapshot.GetText(tokenLocation.Location, tokenLocation.Length); } } lineOfPreviousToken = line.LineNumber; // Line where the last not white-space token is located } } if (currentRegion != null && _startRegionTokens.Contains(currentRegion.StartToken)) { newRegions.Add(new Region() { StartToken = currentRegion.StartToken, StartLine = currentRegion.StartLine, TableName = currentRegion.TableName, MeasureName = currentRegion.MeasureName, EndLine = lineOfPreviousToken }); } // Determine the changed span, and send a changed event with the new spans List <Span> oldSpans = new List <Span>(this.regions.Select(r => AsSnapshotSpan(r, this.snapshot) .TranslateTo(newSnapshot, SpanTrackingMode.EdgeExclusive) .Span)); List <Span> newSpans = new List <Span>(newRegions.Select(r => AsSnapshotSpan(r, newSnapshot).Span)); NormalizedSpanCollection oldSpanCollection = new NormalizedSpanCollection(oldSpans); NormalizedSpanCollection newSpanCollection = new NormalizedSpanCollection(newSpans); // The changed regions are regions that appear in one set or the other, but not both. NormalizedSpanCollection removed = NormalizedSpanCollection.Difference(oldSpanCollection, newSpanCollection); int changeStart = int.MaxValue; int changeEnd = -1; if (removed.Count > 0) { changeStart = removed[0].Start; changeEnd = removed[removed.Count - 1].End; } if (newSpans.Count > 0) { changeStart = Math.Min(changeStart, newSpans[0].Start); changeEnd = Math.Max(changeEnd, newSpans[newSpans.Count - 1].End); } this.snapshot = newSnapshot; this.regions = newRegions; if (changeStart <= changeEnd) { ITextSnapshot snap = this.snapshot; if (this.TagsChanged != null) { this.TagsChanged(this, new SnapshotSpanEventArgs( new SnapshotSpan(this.snapshot, Span.FromBounds(changeStart, changeEnd)))); } } }