Esempio n. 1
0
    public override IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset)
    {
      try
      {
        var timer = Stopwatch.StartNew();
        TimeSpan = timer.Elapsed;

        var result = new List<NewFolding>();
        foreach (var o in Outlining)
        {
          var newFolding = new NewFolding
          {
            DefaultClosed = o.IsDefaultCollapsed,
            StartOffset   = o.Span.StartPos,
            EndOffset     = o.Span.EndPos
          };
          result.Add(newFolding);
        }
        result.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset));

        firstErrorOffset = 0;
        return result;
      }
      catch (Exception ex)
      {
        Debug.WriteLine(ex.GetType().Name + ":" + ex.Message);
        firstErrorOffset = 0;
        return Enumerable.Empty<NewFolding>();
      }
    }
        /// <summary>
        /// Create <see cref="NewFolding"/>s for the specified document.
        /// </summary>
        public IEnumerable<NewFolding> CreateNewFoldings(ITextSource document)
        {
            List<NewFolding> newFoldings = new List<NewFolding>();

            Stack<int> startOffsets = new Stack<int>();
            int lastNewLineOffset = 0;
            char openingBrace = OpeningBrace;
            char closingBrace = ClosingBrace;

            foreach (var startKeyword in foldingKeywords.Keys)
            {
                int lastKeywordPos = 0;
                int pos = 0;

                while ((pos = document.Text.IndexOf(startKeyword, pos)) > lastKeywordPos)
                {
                    int endOffset = document.Text.IndexOf(foldingKeywords[startKeyword], pos);

                    if (endOffset > pos)
                    {
                        var offset = document.Text.IndexOf("\r\n", pos);
                        var name = document.Text.Substring(pos + 8, offset - (pos + 8));
                        var folding = new NewFolding(pos, endOffset + 10);
                        folding.Name = name;

                        // Add the folding
                        newFoldings.Add(folding);
                    }

                    lastKeywordPos = pos;
                }
            }

            for (int i = 0; i < document.TextLength; i++)
            {
                char c = document.GetCharAt(i);
                if (c == openingBrace)
                {
                    startOffsets.Push(i);
                }
                else if (c == closingBrace && startOffsets.Count > 0)
                {
                    int 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;
                }
            }

            newFoldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset));

            return newFoldings;
        }
Esempio n. 3
0
		NewFolding AddFolding(TextLocation start, TextLocation end, bool isDefinition = false)
		{
			if (end.Line <= start.Line || start.IsEmpty || end.IsEmpty)
				return null;
			NewFolding folding = new NewFolding(GetOffset(start), GetOffset(end));
			folding.IsDefinition = isDefinition;
			foldings.Add(folding);
			return folding;
		}
Esempio n. 4
0
        /// <summary>
        /// Create <see cref="NewFolding"/>s for the specified document.
        /// </summary>
        public IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset)
        {
            firstErrorOffset = -1;

            var foldings = new List<NewFolding>();
            var stack = new Stack<int>();

            foreach (var line in document.Lines)
            {
                var text = document.GetText(line.Offset, line.Length);

                // комментарии пропускаем
                if (commentPattern.IsMatch(text))
                    continue;

                foreach (Match match in startPattern.Matches(text))
                {
                    var element = match.Groups["start"];
                    if (element.Success)
                    {
                        stack.Push(line.EndOffset);
                    }
                }

                foreach (Match match in endPattern.Matches(text))
                {
                    var element = match.Groups["end"];
                    if (element.Success)
                    {
                        if (stack.Count > 0)
                        {
                            var first = stack.Pop();
                            var folding = new NewFolding(first, line.EndOffset);
                            foldings.Add(folding);
                        }
                        else
                        {
                            firstErrorOffset = line.Offset;
                        }
                    }
                }
            }

            if (stack.Count > 0)
            {
                firstErrorOffset = stack.Pop();
            }

            foldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset));
            return foldings;
        }
        public void UpdateFoldings(FoldingManager fm, IEnumerable<String> linesadded)
        {

            var last = fm.GetNextFolding(lastfold.StartOffset);
            foreach (var line in linesadded)
            {
                 var M = r.Match(line);
                 if (M.Success)
                 {
                     endfold = n + line.Length;
                     if (!prev)
                     {
                         prev = true;
                         startfold = n;
                         lastfold = new NewFolding(startfold, endfold);
                         fm.CreateFolding(startfold, endfold).Title = "FileSync Complete";
                     }
                     else
                     {
                         last.EndOffset = endfold;
                     }
                    
                     
                 }
                 else
                 {
                     if (prev)
                     {
                       
                             lastfold = new NewFolding(startfold, endfold);
                             fm.CreateFolding(startfold, endfold).Title = "FileSync Complete";
                         
                     }
                         
                     prev = false;
                 }

                 linenr++;
                 n += line.Length+1;
            }
        }
        /// <summary>
        /// Create new folding for a givem visual basic string representation.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="lowerCaseText"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public IEnumerable<NewFolding> GetFoldings(string text, string lowerCaseText, string keyword)
        {
            const char vbCr = '\r';
              const char vbLf = '\n';
              ////const string vbCrLf = "\r\n";

              List<NewFolding> foldings = new List<NewFolding>();

              string closingKeyword = "end " + keyword;
              int closingKeywordLength = closingKeyword.Length;

              keyword += " ";
              int keywordLength = keyword.Length;

              Stack<int> startOffsets = new Stack<int>();

              for (int i = 0; i <= text.Length - closingKeywordLength; i++)
              {
            if (lowerCaseText.Substring(i, keywordLength) == keyword)
            {
              int k = i;
              if (k > 0)
              {
            int lastLetterPos = i;
            while (!(text[k - 1] == vbLf || text[k - 1] == vbCr))
            {
              if (char.IsLetter(text[k]))
                lastLetterPos = k;

              k -= 1;

              if (k == 0)
                break;
            }

            //if (lastLetterPos > k) Dirkster99 Not sure why this is necessary
            //  k = lastLetterPos;
              }

              startOffsets.Push(k);
            }
            else if (lowerCaseText.Substring(i, closingKeywordLength) == closingKeyword)
            {
              int startOffset = startOffsets.Pop();
              NewFolding newFolding = new NewFolding(startOffset, i + closingKeywordLength);
              int p = text.IndexOf(vbLf, startOffset);

              if (p == -1)
            p = text.IndexOf(vbCr, startOffset);

              if (p == -1)
            p = text.Length - 1;

              newFolding.Name = text.Substring(startOffset, p - startOffset);
              foldings.Add(newFolding);
            }
              }

              return foldings;
        }
Esempio n. 7
0
		public override void VisitPreProcessorDirective(PreProcessorDirective preProcessorDirective)
		{
			switch (preProcessorDirective.Type) {
				case PreProcessorDirectiveType.Region:
					NewFolding folding = new NewFolding();
					folding.DefaultClosed = true;
					folding.Name = preProcessorDirective.Argument;
					folding.StartOffset = GetOffset(preProcessorDirective.StartLocation);
					regions.Push(folding);
					break;
				case PreProcessorDirectiveType.Endregion:
					if (regions.Count > 0) {
						folding = regions.Pop();
						folding.EndOffset = GetOffset(preProcessorDirective.EndLocation);
						foldings.Add(folding);
					}
					break;
			}
		}
Esempio n. 8
0
        /// <summary>
        /// Create <see cref="NewFolding"/>s for the specified document.
        /// </summary>
        public IEnumerable<NewFolding> CreateNewFoldings(ITextSource document)
        {
            var newFoldings = new List<NewFolding>();

            var template = FoldingTemplates[0];
            var regexOpenFolding = new Regex(template.OpeningPhrase);
            var matchesOpenFolding = regexOpenFolding.Matches(document.Text);

            var regexCloseFolding = new Regex(template.ClosingPhrase);
            var matchesCloseFolding = regexCloseFolding.Matches(document.Text);

            var currentOpenIndex = 0;
            for (var i = 0; i < matchesCloseFolding.Count && currentOpenIndex < matchesOpenFolding.Count; i++)
            {
                if (matchesOpenFolding[currentOpenIndex].Index >= matchesCloseFolding[i].Index)
                    continue;
                var folding = new NewFolding(matchesOpenFolding[currentOpenIndex].Index + 1,
                    matchesCloseFolding[i].Index + 10)
                {
                    DefaultClosed = template.IsDefaultFolded,
                    Name = template.Name
                };
                newFoldings.Add(folding);
                while (currentOpenIndex < matchesOpenFolding.Count &&
                    matchesOpenFolding[currentOpenIndex].Index < matchesCloseFolding[i].Index)
                {
                    currentOpenIndex++;
                }
            }
            return newFoldings;
        }
		public void GenerateFolds_MethodCalled_FoldsFromFoldParserUsedToUpdateTextEditor()
		{
			CreateFakeTextEditor();
			CreateFoldGenerator();
			string html = "<p></p>";
			SetTextInTextEditor(html);
			var expectedFolds = new NewFolding[] {
				new NewFolding()
			};
			SetFoldsToReturnFromFoldParserForHtml(expectedFolds, html);
			
			foldGenerator.GenerateFolds();
			
			fakeTextEditor.AssertWasCalled(textEditor => textEditor.UpdateFolds(expectedFolds));
		}
Esempio n. 10
0
		void SetFoldsToReturnFromFoldParserForHtml(NewFolding[] expectedFolds, string html)
		{
			fakeFoldParser.Stub(parser => parser.GetFolds(html))
				.Return(expectedFolds);
		}
Esempio n. 11
0
		public IEnumerable<NewFolding> CreateNewFoldings(ITextSource document)
		{
			List<NewFolding> newFoldings = new List<NewFolding>();
            _procList = new List<ProcListItem>();

            int startPos = 0;
            int len = document.TextLength;

            int currentStart = 0;

            bool MethodIsOpen = false;
            string MethodName ="";
            string EndToken = null;

            int PreCommentStart = -1;

            //var Reader = document.CreateReader();

            string FullText = document.Text;
            int DocLine = 0;
            int MethodStart = 0;

            const string kProcStart = "ПРОЦЕДУРА";
            const string kProcEnd = "КОНЕЦПРОЦЕДУРЫ";
            const string kFuncStart = "ФУНКЦИЯ";
            const string kFuncEnd = "КОНЕЦФУНКЦИИ";

            char[] trimArr = new char[]{' ','\t'};

            do
            {
                int prev_start = startPos;
                string lineText = ReadLine(FullText, ref startPos);
                
                DocLine++;

                if (lineText == null)
                {
                    break;
                }
                
                TextFragment tf = new TextFragment();
                tf.offset = prev_start;
                tf.len = lineText.Length;

                //startPos += lineText.Length + 2;

                if (!MethodIsOpen)
                {
                    bool CommentBreak = false;
                    
                    if (lineText.StartsWith("//"))
                    {
                        if (PreCommentStart < 0)
                        {
                            PreCommentStart = tf.offset + tf.len;
                        }
                    }
                    else
                    {
                        CommentBreak = true;
                    }
                    
                    if (LineIsKeyword(lineText.TrimStart(trimArr), kProcStart))
                    {
                        MethodIsOpen = true;
                        MethodName = ScanForParamList(FullText, prev_start+kProcStart.Length);
                        EndToken = kProcEnd;
                        MethodStart = DocLine;
                    }
                    else if(LineIsKeyword(lineText.TrimStart(trimArr), kFuncStart))
                    {
                        MethodIsOpen = true;
                        MethodName = ScanForParamList(FullText, prev_start + kFuncStart.Length);
                        EndToken = kFuncEnd;
                        MethodStart = DocLine;
                    }

                    if (MethodIsOpen)
                    {
                        currentStart = tf.offset + tf.len;

                        if (PreCommentStart >= 0)
                        {
                            var Folding = new NewFolding(PreCommentStart, tf.offset-1);
                            newFoldings.Add(Folding);
                            PreCommentStart = -1;
                        }
                    }
                    else if(CommentBreak)
                    {
                        PreCommentStart = -1;
                    }
                    
                }
                else if (LineIsKeyword(lineText.TrimStart(trimArr), EndToken))
                {
                    var Folding = new NewFolding(currentStart, tf.offset + tf.len);
                    newFoldings.Add(Folding);

                    if (MethodName != "")
                    {
                        ProcListItem pli = new ProcListItem();
                        pli.Name = MethodName;
                        pli.StartLine = MethodStart;
                        pli.EndLine = DocLine;
                        pli.ListIndex = _procList.Count;

                        _procList.Add(pli);
                        
                        MethodName = "";
                    }

                    MethodIsOpen = false;
                }
                
            }
            while (true);

			return newFoldings;
		}
Esempio n. 12
0
		NewFolding ConvertToNewFold(FoldingRegion foldingRegion)
		{
			NewFolding newFold = new NewFolding();
			
			newFold.Name = foldingRegion.Name;
			newFold.StartOffset = GetStartOffset(foldingRegion.Region);
			newFold.EndOffset = GetEndOffset(foldingRegion.Region);
			
			return newFold;
		}
Esempio n. 13
0
 public static string ConvertToString(NewFolding fold)
 {
     return String.Format("Name: '{0}' StartOffset: {1} EndOffset: {2} DefaultClosed: {3}",
         fold.Name, fold.StartOffset, fold.EndOffset, fold.DefaultClosed);
 }
Esempio n. 14
0
		NewFolding CreateTestNewFolding()
		{
			NewFolding expectedFold = new NewFolding();
			expectedFold.DefaultClosed = false;
			expectedFold.StartOffset = 5;
			expectedFold.EndOffset = 10;
			expectedFold.Name = "test";
			
			return expectedFold;
		}
        public override IEnumerable<NewFolding> CreateNewFoldings(TextDocument document, out int firstErrorOffset)
        {
            firstErrorOffset = -1;
            
           var folds =  new List<NewFolding>();

            
            linenr = 1;
            prev = false;
            startfold = 0; endfold = 0;
            n = 0;
            var l = document.GetLineByNumber(linenr);
           
            int sep = 0;
           
            do
            {
                int N = l.TotalLength;
                var line = document.GetText(n, N);
                var M = r.Match(line);
                if (M.Success)
                {
                    if (!prev)
                    {
                        prev = true;
                        startfold = n;
                        
                    }
                    endfold = n + l.Length;   

                }
                else
                {

                    if (prev)
                        folds.Add(lastfold=new NewFolding{ StartOffset=startfold, EndOffset= endfold, Name="FileSync Complete", DefaultClosed=true});
                    prev = false;
                    
                   
                }



                linenr++;
                n += N;
            }
            while ((l = l.NextLine) != null);


            return folds;
        }
Esempio n. 16
0
        /// <inheritdoc/>
        protected override IEnumerable<NewFolding> CreateNewFoldings(
            AvalonEditDocument document, out int firstErrorOffset)
        {
            firstErrorOffset = -1;

            List<NewFolding> foldings = new List<NewFolding>();

            Stack<NewFolding> activeFoldings = new Stack<NewFolding>();
            Stack<int> activeIndentations = new Stack<int>();

            NewFolding activeFolding = null;
            int activeIndentation = -1;
            int endOffsetOfLastNonEmptyLine = 0;
            for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++)
            {
                var line = document.GetLineByNumber(lineNumber);

                // Determine indentation of line.
                int offset = line.Offset;
                int indentation = 0;
                bool isLineEmpty = true;
                for (int i = 0; i < line.Length; i++)
                {
                    if (char.IsWhiteSpace(document.GetCharAt(offset + i)))
                    {
                        indentation++;
                    }
                    else
                    {
                        // Found the first non-white space character.
                        isLineEmpty = false;
                        break;
                    }
                }

                // Skip empty lines.
                if (isLineEmpty)
                    continue;

                // If the indentation is less than the previous, then we close the last active
                // folding.
                if (indentation < activeIndentation)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    activeFolding.EndOffset = endOffsetOfLastNonEmptyLine;

                    // Keep foldings which span more than one line.
                    if (document.GetLineByOffset(activeFolding.StartOffset) != document.GetLineByOffset(activeFolding.EndOffset))
                        foldings.Add(activeFolding);

                    if (activeFoldings.Count > 0)
                    {
                        activeFolding = activeFoldings.Pop();
                        activeIndentation = activeIndentations.Pop();
                    }
                    else
                    {
                        activeFolding = null;
                        activeIndentation = -1;
                    }

                    // Test same line again. The previous folding could also end on this line.
                    lineNumber--;
                    continue;
                }

                endOffsetOfLastNonEmptyLine = line.EndOffset;

                // If the indentation is larger than the previous indentation, we start a new
                // folding.
                if (indentation > 0 && indentation > activeIndentation)
                {
                    // Store current folding on stack.
                    if (activeFolding != null)
                    {
                        activeFoldings.Push(activeFolding);
                        activeIndentations.Push(activeIndentation);
                    }

                    activeFolding = new NewFolding { StartOffset = offset + indentation, };
                    activeIndentation = indentation;
                }
            }

            // Close all open foldings.
            while (activeFoldings.Count > 0)
            {
                var folding = activeFoldings.Pop();
                folding.EndOffset = endOffsetOfLastNonEmptyLine;
                foldings.Add(folding);
            }

            foldings.Sort((a, b) => a.StartOffset.CompareTo(b.StartOffset));
            return foldings;
        }
Esempio n. 17
0
        private NewFolding CreateElementFold(TextDocument document, HtmlNode node)
        {
            NewFolding newFold = new NewFolding();

            if (node.Line != node.EndNode.Line)
            {
                if (this.ShowAttributesWhenFolded && node.HasAttributes)
                    newFold.Name = String.Concat("<", node.Name, " ", GetAttributeFoldText(node), ">");
                else
                    newFold.Name = String.Concat("<", node.Name, ">");

                newFold.StartOffset = document.GetOffset(node.Line, node.LinePosition - 1);
                newFold.EndOffset = document.GetOffset(node.EndNode.Line, node.EndNode.LinePosition + node.OriginalName.Length + 3);
            }

            return newFold;
        }