first commit
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
// This file is part of CodingEditor.
|
||||
// Note: This project is derived from Peter Project
|
||||
// (hosted on sourceforge and codeplex)
|
||||
//
|
||||
// Copyright (c) 2008-2009, CE Team
|
||||
//
|
||||
// This program is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation; either version 2
|
||||
// of the License, or (at your option) any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICSharpCode.TextEditor.Document;
|
||||
|
||||
namespace ICSharpCode.TextEditor.Src.Document.FoldingStrategy
|
||||
{
|
||||
public class JSONFoldingStrategy : IFoldingStrategy
|
||||
{
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Generates the foldings for our document.
|
||||
/// </summary>
|
||||
/// <param name="document">The current document.</param>
|
||||
/// <param name="fileName">The filename of the document.</param>
|
||||
/// <param name="parseInformation">Extra parse information, not used in this sample.</param>
|
||||
/// <returns>A list of FoldMarkers.</returns>
|
||||
public List<FoldMarker> GenerateFoldMarkers(IDocument document, string fileName, object parseInformation)
|
||||
{
|
||||
var list = new List<FoldMarker>();
|
||||
var startLines = new Stack<int>();
|
||||
|
||||
// Create foldmarkers for the whole document, enumerate through every line.
|
||||
for (int i = 0; i < document.TotalNumberOfLines; i++)
|
||||
{
|
||||
var seg = document.GetLineSegment(i);
|
||||
int offs, end = seg.Length + seg.Offset;
|
||||
char c;
|
||||
for (
|
||||
offs = seg.Offset;
|
||||
offs < end;
|
||||
offs++)
|
||||
{
|
||||
|
||||
c = document.GetCharAt(offs);
|
||||
if (seg.Words.Any(w => w.IsDelimiter && w.Offset == offs - seg.Offset && w.Word == c.ToString()))
|
||||
{
|
||||
if (c == '{')
|
||||
{
|
||||
int offsetOfClosingBracket = SearchBracketForward(document, i, offs, '{', '}');
|
||||
if (offsetOfClosingBracket > 0)
|
||||
{
|
||||
int length = offsetOfClosingBracket - offs + 1;
|
||||
list.Add(new FoldMarker(document, offs, length, "{...}", false));
|
||||
}
|
||||
}
|
||||
if (c == '[')
|
||||
{
|
||||
int offsetOfClosingBracket = SearchBracketForward(document, i, offs, '[', ']');
|
||||
if (offsetOfClosingBracket > 0)
|
||||
{
|
||||
int length = offsetOfClosingBracket - offs + 1;
|
||||
list.Add(new FoldMarker(document, offs, length, "[...]", false));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
|
||||
private int SearchBracketForward(IDocument document, int currLine, int currOffset, char openBracket, char closingBracket)
|
||||
{
|
||||
// Create foldmarkers for the whole document, enumerate through every line.
|
||||
|
||||
int brackets = 1, spaceCount = 0;
|
||||
for (int i = currLine; i < document.TotalNumberOfLines; i++)
|
||||
{
|
||||
var seg = document.GetLineSegment(i);
|
||||
int offs, end = seg.Length + seg.Offset;
|
||||
char c;
|
||||
for (
|
||||
offs = i == currLine ? currOffset + 1 : seg.Offset;
|
||||
offs < end;
|
||||
offs++)
|
||||
{
|
||||
|
||||
c = document.GetCharAt(offs);
|
||||
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') spaceCount++;
|
||||
if (seg.Words.Any(w => w.IsDelimiter && w.Offset == offs - seg.Offset && w.Word == c.ToString()))
|
||||
{
|
||||
if (c == openBracket)
|
||||
{
|
||||
++brackets;
|
||||
}
|
||||
else if (c == closingBracket)
|
||||
{
|
||||
--brackets;
|
||||
if (brackets == 0)
|
||||
{
|
||||
if (offs - spaceCount - 1 == currOffset)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return offs;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#endregion Methods
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user