Пример #1
0
        private bool PreviousLineWasClosed(int lineIndex)
        {
            TextEditorCore textCore = AlternateEditorCore;

            textCore = (null != textCore ? textCore : TextEditorCore.Instance);

            lineIndex = lineIndex - 1;
            while (lineIndex >= 0)
            {
                string lineContent = lineList[lineIndex];
                if (null == textCore)
                {
                    lineIndex = lineIndex - 1;
                }
                else
                {
                    if (textCore.IsLineCommented(lineIndex--, false))
                    {
                        continue; // Ignore commented line.
                    }
                }

                // Strip off the trailing comment if there's any.
                int commentIndex = lineContent.IndexOf("//");
                if (commentIndex != -1)
                {
                    lineContent = lineContent.Substring(0, commentIndex);
                }

                if (string.IsNullOrWhiteSpace(lineContent))
                {
                    continue; // Skipping past all blank lines.
                }
                // If the line is closed, then return it.
                if (lineClosureRule.IsMatch(lineContent))
                {
                    return(true);
                }

                // If the line wasn't closed but it was an open
                // bracket, then treat it as being closed.
                return(scopeOpeningRule.IsMatch(lineContent));
            }

            return(true);
        }
Пример #2
0
 public static ITextEditorCore CreateTextEditorCore(ExecutionStateChangedHandler handler)
 {
     return(TextEditorCore.CreateSingleton(handler));
 }
Пример #3
0
        public void Setup()
        {
            string fileContent =
                "hello\n" +
                "\n" +
                "\t\tcruel\n" +
                "\tworld";

            testSolution = Solution.CreateTemporary();
            testSolution.AddNewScript(fileContent);
            testSolution.ActivateScript(0);

            textCore = TextEditorCore.CreateTemporary();
            textCore.ChangeScript(0);
            textCore.ParseScriptImmediate();
        }
Пример #4
0
        private string FormatLine(int lineIndex)
        {
            string         content  = lineList[lineIndex];
            TextEditorCore textCore = AlternateEditorCore;

            textCore = (null != textCore ? textCore : TextEditorCore.Instance);
            if (null != textCore && (textCore.IsLineCommented(lineIndex, true)))
            {
                return(content);
            }

            string comment      = string.Empty;
            int    commentIndex = content.IndexOf("//");

            if (commentIndex != -1)
            {
                // Strip off comment momentarily.
                comment = content.Substring(commentIndex);
                content = content.Substring(0, commentIndex);
            }

            string result            = string.Empty;
            bool   isInStringSegment = false;

            int index       = 0;
            int quoteOffset = ScanForNextQuote(content, 0);

            while (index < content.Length)
            {
                if (quoteOffset < 0)
                {
                    quoteOffset = content.Length;
                }

                if (false == isInStringSegment)
                {
                    // If we're parsing a regular code segment (i.e. not a
                    // string literal), then hand it down for formatting.
                    string segment = content.Substring(index, quoteOffset - index);
                    result += FormatSegment(segment);
                }
                else
                {
                    // If we're currently in a string segment, then
                    // move ahead to include the closing quote character.
                    quoteOffset = quoteOffset + 1;

                    // This is true when the string ends with an open bracket.
                    if (quoteOffset > content.Length)
                    {
                        quoteOffset = content.Length;
                    }

                    string segment = content.Substring(index, quoteOffset - index);
                    result += segment;
                }

                index             = quoteOffset;
                quoteOffset       = ScanForNextQuote(content, quoteOffset + 1);
                isInStringSegment = !isInStringSegment;
            }

            // Final excessive space trimming rules.
            result = spaceNormalizationRule.Replace(result, " ");
            result = spaceTrimmingRule.Replace(result, "");

            // Restore comment towards the end of the string (if any).
            if (!string.IsNullOrEmpty(result) && (!string.IsNullOrEmpty(comment)))
            {
                result = result + " "; // We have both content and comment, add space!
            }
            content = result + comment;
            return(PostFormatProcessing(lineIndex, content));
        }