/// <summary> /// Create <see cref="NewFolding"/>s for the specified document. /// </summary> public IEnumerable <NewFolding> CreateNewFoldings(ITextSource document) { var newFoldings = new List <NewFolding>(); var startOffsets = new Stack <int>(); var lastNewLineOffset = 0; var currentIndex = 0; var json = document.Text; //for (var i = 0; i < document.TextLength; i++) //{ // var c = document.GetCharAt(i); // if (c == '{') // { // startOffsets.Push(i); // } // else if (c == '}' && startOffsets.Count > 0) // { // var startOffset = startOffsets.Pop(); // // don't fold if opening and closing brace are on the same line // if (startOffset < lastNewLineOffset) // { // newFoldings.Add(new NewFolding(startOffset, i + 1)); // } // } // else if (c == '\n' || c == '\r') // { // lastNewLineOffset = i + 1; // } //} while (true) { //¿ªÊ¼±ê¼Ç var index = json.IndexOfAny(Braces, currentIndex); if (index == -1) { break; } currentIndex = index + 1; var brace = json.Substring(index, 1); if (OpeningBrace.Contains(brace)) { startOffsets.Push(index); } else if (ClosingBrace.Contains(brace) && startOffsets.Any()) { var start = startOffsets.Pop(); if (start < lastNewLineOffset) { newFoldings.Add(new NewFolding(start, index + 1)); } } else if (Line.Contains(brace)) { lastNewLineOffset = index + 1; } } newFoldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset)); return(newFoldings); }
private int countEndBlock(string text) { int res = 0; List <string> words = Folding.VerilogFoldingStrategy.MySplit(text); foreach (string s in words) { if (ClosingBrace.Contains(s)) { res++; } } return(res); }
private void Start(CancellationToken cancellationToken) { // this is where the caret should go after the change SnapshotPoint pos = TextView.Caret.Position.BufferPosition; ITrackingPoint beforeTrackingPoint = pos.Snapshot.CreateTrackingPoint(pos.Position, PointTrackingMode.Negative); ITextSnapshot snapshot = SubjectBuffer.CurrentSnapshot; SnapshotPoint closingSnapshotPoint = ClosingPoint.GetPoint(snapshot); if (closingSnapshotPoint.Position < 1) { Debug.Fail("The closing point was not found at the expected position."); EndSession(); return; } SnapshotPoint openingSnapshotPoint = closingSnapshotPoint.Subtract(1); if (openingSnapshotPoint.GetChar() != OpeningBrace) { // there is a bug in editor brace completion engine on projection buffer that already fixed in vs_pro. until that is FIed to use // I will make this not to assert // Debug.Fail("The opening brace was not found at the expected position."); EndSession(); return; } OpeningPoint = snapshot.CreateTrackingPoint(openingSnapshotPoint, PointTrackingMode.Positive); var document = snapshot.GetOpenDocumentInCurrentContextWithChanges(); if (!_session.CheckOpeningPoint(this, cancellationToken)) { EndSession(); return; } using (ITextUndoTransaction undo = CreateUndoTransaction()) { // insert the closing brace using (ITextEdit edit = SubjectBuffer.CreateEdit()) { edit.Insert(closingSnapshotPoint, ClosingBrace.ToString()); if (edit.HasFailedChanges) { Debug.Fail("Unable to insert closing brace"); // exit without setting the closing point which will take us off the stack edit.Cancel(); undo.Cancel(); return; } else { snapshot = edit.Apply(); } } SnapshotPoint beforePoint = beforeTrackingPoint.GetPoint(TextView.TextSnapshot); // switch from positive to negative tracking so it stays against the closing brace ClosingPoint = SubjectBuffer.CurrentSnapshot.CreateTrackingPoint(ClosingPoint.GetPoint(snapshot), PointTrackingMode.Negative); Debug.Assert(ClosingPoint.GetPoint(snapshot).Position > 0 && (new SnapshotSpan(ClosingPoint.GetPoint(snapshot).Subtract(1), 1)) .GetText().Equals(ClosingBrace.ToString()), "The closing point does not match the closing brace character"); // move the caret back between the braces TextView.Caret.MoveTo(beforePoint); _session.AfterStart(this, cancellationToken); undo.Complete(); } }