Ejemplo n.º 1
0
        public static string GetWordAround(this ICSharpCode.AvalonEdit.Document.TextDocument doc, int offset)
        {
            if (offset < 0 || offset >= doc.TextLength)
            {
                return(string.Empty);
            }
            int start, end;

            //find start
            for (start = offset; start >= 0; start--)
            {
                var c = doc.GetCharAt(start);
                if (!char.IsLetterOrDigit(c) && c != '_')
                {
                    start++;
                    break;
                }
            }
            //find end
            for (end = start; end < doc.TextLength; end++)
            {
                var c = doc.GetCharAt(end);
                if (!char.IsLetterOrDigit(c) && c != '_')
                {
                    break;
                }
            }
            return(doc.GetText(start, end - start));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Class constructor
        /// </summary>
        /// <param name="document"></param>
        /// <param name="offset"></param>
        /// <returns></returns>
        public BracketSearchResult SearchBracket(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset)
        {
            if (offset > 0)
            {
                char c           = document.GetCharAt(offset - 1);
                int  index       = openingBrackets.IndexOf(c);
                int  otherOffset = -1;
                if (index > -1)
                {
                    otherOffset = SearchBracketForward(document, offset, openingBrackets[index], closingBrackets[index]);
                }

                index = closingBrackets.IndexOf(c);
                if (index > -1)
                {
                    otherOffset = SearchBracketBackward(document, offset - 2, openingBrackets[index], closingBrackets[index]);
                }

                if (otherOffset > -1)
                {
                    return(new BracketSearchResult(Math.Min(offset - 1, otherOffset), 1,
                                                   Math.Max(offset - 1, otherOffset), 1));
                }
            }

            return(null);
        }
Ejemplo n.º 3
0
        public static IEnumerable <string> AllIdents(this ICSharpCode.AvalonEdit.Document.TextDocument doc, int minLength = 2)
        {
            var  builder    = new StringBuilder();
            bool isString   = false;
            char stringchar = '\0';

            for (int i = 0; i < doc.TextLength; i++)
            {
                var c = doc.GetCharAt(i);
                if (isString)
                {
                    if (c == stringchar)
                    {
                        isString = false;
                    }
                }
                else if ((builder.Length == 0 ? char.IsLetter(c) : char.IsLetterOrDigit(c)) || c == '_')
                {
                    builder.Append(c);
                }
                else if (c == '"' || c == '\'')
                {
                    stringchar = c;
                    isString   = true;
                }
                else
                {
                    if (builder.Length >= minLength)
                    {
                        yield return(builder.ToString());
                    }
                    builder.Clear();
                }
            }
        }
Ejemplo n.º 4
0
        private int QuickSearchBracketForward(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset, char openBracket, char closingBracket)
        {
            int brackets = 1;

            // try "quick find" - find the matching bracket if there is no string/comment in the way
            for (int i = offset; i < document.TextLength; ++i)
            {
                char ch = document.GetCharAt(i);
                if (ch == openBracket)
                {
                    ++brackets;
                }
                else if (ch == closingBracket)
                {
                    --brackets;
                    if (brackets == 0)
                    {
                        return(i);
                    }
                }
                else if (ch == '"')
                {
                    break;
                }
                else if (ch == '\'')
                {
                    break;
                }
                else if (ch == '/' && i > 0)
                {
                    if (document.GetCharAt(i - 1) == '/')
                    {
                        break;
                    }
                }
                else if (ch == '*' && i > 0)
                {
                    if (document.GetCharAt(i - 1) == '/')
                    {
                        break;
                    }
                }
            }
            return(-1);
        }
Ejemplo n.º 5
0
 private static int ScanLineStart(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset)
 {
     for (int i = offset - 1; i > 0; --i)
     {
         if (document.GetCharAt(i) == '\n')
         {
             return(i + 1);
         }
     }
     return(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);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Gets the type of code at offset.<br/>
        /// 0 = Code,<br/>
        /// 1 = Comment,<br/>
        /// 2 = String<br/>
        /// Block comments and multiline strings are not supported.
        /// </summary>
        private static int GetStartType(ICSharpCode.AvalonEdit.Document.TextDocument document, int linestart, int offset)
        {
            bool inString = false;
            bool inChar   = false;
            bool verbatim = false;
            int  result   = 0;

            for (int i = linestart; i < offset; i++)
            {
                switch (document.GetCharAt(i))
                {
                case '/':
                    if (!inString && !inChar && i + 1 < document.TextLength)
                    {
                        if (document.GetCharAt(i + 1) == '/')
                        {
                            result = 1;
                        }
                    }
                    break;

                case '"':
                    if (!inChar)
                    {
                        if (inString && verbatim)
                        {
                            if (i + 1 < document.TextLength && document.GetCharAt(i + 1) == '"')
                            {
                                ++i;              // skip escaped quote
                                inString = false; // let the string go on
                            }
                            else
                            {
                                verbatim = false;
                            }
                        }
                        else if (!inString && i > 0 && document.GetCharAt(i - 1) == '@')
                        {
                            verbatim = true;
                        }
                        inString = !inString;
                    }
                    break;

                case '\'':
                    if (!inString)
                    {
                        inChar = !inChar;
                    }
                    break;

                case '\\':
                    if ((inString && !verbatim) || inChar)
                    {
                        ++i; // skip next character
                    }
                    break;
                }
            }

            return((inString || inChar) ? 2 : result);
        }
Ejemplo n.º 8
0
        private int SearchBracketForward(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset, char openBracket, char closingBracket)
        {
            bool inString = false;
            bool inChar   = false;
            bool verbatim = false;

            bool lineComment  = false;
            bool blockComment = false;

            if (offset < 0)
            {
                return(-1);
            }

            // first try "quick find" - find the matching bracket if there is no string/comment in the way
            int quickResult = QuickSearchBracketForward(document, offset, openBracket, closingBracket);

            if (quickResult >= 0)
            {
                return(quickResult);
            }

            // we need to parse the line from the beginning, so get the line start position
            int linestart = ScanLineStart(document, offset);

            // we need to know where offset is - in a string/comment or in normal code?
            // ignore cases where offset is in a block comment
            int starttype = GetStartType(document, linestart, offset);

            if (starttype != 0)
            {
                return(-1);          // start position is in a comment/string
            }
            int brackets = 1;

            while (offset < document.TextLength)
            {
                char ch = document.GetCharAt(offset);
                switch (ch)
                {
                case '\r':
                case '\n':
                    lineComment = false;
                    inChar      = false;
                    if (!verbatim)
                    {
                        inString = false;
                    }
                    break;

                case '/':
                    if (blockComment)
                    {
                        Debug.Assert(offset > 0);
                        if (document.GetCharAt(offset - 1) == '*')
                        {
                            blockComment = false;
                        }
                    }
                    if (!inString && !inChar && offset + 1 < document.TextLength)
                    {
                        if (!blockComment && document.GetCharAt(offset + 1) == '/')
                        {
                            lineComment = true;
                        }
                        if (!lineComment && document.GetCharAt(offset + 1) == '*')
                        {
                            blockComment = true;
                        }
                    }
                    break;

                case '"':
                    if (!(inChar || lineComment || blockComment))
                    {
                        if (inString && verbatim)
                        {
                            if (offset + 1 < document.TextLength && document.GetCharAt(offset + 1) == '"')
                            {
                                ++offset;         // skip escaped quote
                                inString = false; // let the string go
                            }
                            else
                            {
                                verbatim = false;
                            }
                        }
                        else if (!inString && offset > 0 && document.GetCharAt(offset - 1) == '@')
                        {
                            verbatim = true;
                        }
                        inString = !inString;
                    }
                    break;

                case '\'':
                    if (!(inString || lineComment || blockComment))
                    {
                        inChar = !inChar;
                    }
                    break;

                case '\\':
                    if ((inString && !verbatim) || inChar)
                    {
                        ++offset; // skip next character
                    }
                    break;

                default:
                    if (ch == openBracket)
                    {
                        if (!(inString || inChar || lineComment || blockComment))
                        {
                            ++brackets;
                        }
                    }
                    else if (ch == closingBracket)
                    {
                        if (!(inString || inChar || lineComment || blockComment))
                        {
                            --brackets;
                            if (brackets == 0)
                            {
                                return(offset);
                            }
                        }
                    }
                    break;
                }
                ++offset;
            }
            return(-1);
        }
Ejemplo n.º 9
0
        private int SearchBracketBackward(ICSharpCode.AvalonEdit.Document.TextDocument document, int offset, char openBracket, char closingBracket)
        {
            if (offset + 1 >= document.TextLength)
            {
                return(-1);
            }
            // this method parses a c# document backwards to find the matching bracket

            // first try "quick find" - find the matching bracket if there is no string/comment in the way
            int quickResult = QuickSearchBracketBackward(document, offset, openBracket, closingBracket);

            if (quickResult >= 0)
            {
                return(quickResult);
            }

            // we need to parse the line from the beginning, so get the line start position
            int linestart = ScanLineStart(document, offset + 1);

            // we need to know where offset is - in a string/comment or in normal code?
            // ignore cases where offset is in a block comment
            int starttype = GetStartType(document, linestart, offset + 1);

            if (starttype == 1)
            {
                return(-1); // start position is in a comment
            }

            // I don't see any possibility to parse a C# document backwards...
            // We have to do it forwards and push all bracket positions on a stack.
            Stack <int> bracketStack = new Stack <int>();
            bool        blockComment = false;
            bool        lineComment  = false;
            bool        inChar       = false;
            bool        inString     = false;
            bool        verbatim     = false;

            for (int i = 0; i <= offset; ++i)
            {
                char ch = document.GetCharAt(i);
                switch (ch)
                {
                case '\r':
                case '\n':
                    lineComment = false;
                    inChar      = false;
                    if (!verbatim)
                    {
                        inString = false;
                    }
                    break;

                case '/':
                    if (blockComment)
                    {
                        Debug.Assert(i > 0);
                        if (document.GetCharAt(i - 1) == '*')
                        {
                            blockComment = false;
                        }
                    }
                    if (!inString && !inChar && i + 1 < document.TextLength)
                    {
                        if (!blockComment && document.GetCharAt(i + 1) == '/')
                        {
                            lineComment = true;
                        }
                        if (!lineComment && document.GetCharAt(i + 1) == '*')
                        {
                            blockComment = true;
                        }
                    }
                    break;

                case '"':
                    if (!(inChar || lineComment || blockComment))
                    {
                        if (inString && verbatim)
                        {
                            if (i + 1 < document.TextLength && document.GetCharAt(i + 1) == '"')
                            {
                                ++i;              // skip escaped quote
                                inString = false; // let the string go
                            }
                            else
                            {
                                verbatim = false;
                            }
                        }
                        else if (!inString && offset > 0 && document.GetCharAt(i - 1) == '@')
                        {
                            verbatim = true;
                        }
                        inString = !inString;
                    }
                    break;

                case '\'':
                    if (!(inString || lineComment || blockComment))
                    {
                        inChar = !inChar;
                    }
                    break;

                case '\\':
                    if ((inString && !verbatim) || inChar)
                    {
                        ++i; // skip next character
                    }
                    break;

                default:
                    if (ch == openBracket)
                    {
                        if (!(inString || inChar || lineComment || blockComment))
                        {
                            bracketStack.Push(i);
                        }
                    }
                    else if (ch == closingBracket)
                    {
                        if (!(inString || inChar || lineComment || blockComment))
                        {
                            if (bracketStack.Count > 0)
                            {
                                bracketStack.Pop();
                            }
                        }
                    }
                    break;
                }
            }
            if (bracketStack.Count > 0)
            {
                return((int)bracketStack.Pop());
            }
            return(-1);
        }
Ejemplo n.º 10
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;
        }