// used for comment tag formater/inserter
        public override int FormatLine(IFormattableDocument d, int lineNr, int cursorOffset, char ch)
        {
            switch (ch) {
                //case '}':
                //case '{':
                //	return d.FormattingStrategy.IndentLine (d, lineNr);
                case '\n':
                    if (lineNr <= 0) {
                        return IndentLine(d, lineNr);
                    }

                    if (d.AutoInsertCurlyBracket) {
                        string oldLineText = d.GetLineAsString (lineNr - 1);
                        if (oldLineText.EndsWith ("{") && NeedCurlyBracket (d.TextContent)) {
                                d.Insert (cursorOffset, "\n}");
                                IndentLine(d, lineNr + 1);
                        }
                    }

                    string  lineAboveText = d.GetLineAsString (lineNr - 1);

            #if NON_PORTABLE_CODE
                    if (lineAbove.HighlightSpanStack != null && lineAbove.HighlightSpanStack.Count > 0) {
                        if (!((Span)lineAbove.HighlightSpanStack.Peek()).StopEOL) {	// case for /* style comments
                            int index = lineAboveText.IndexOf("/*");

                            if (index > 0) {
                                string indentation = GetIndentation(d, lineNr - 1);
                                for (int i = indentation.Length; i < index; ++ i) {
                                    indentation += ' ';
                                }
                                d.Document.Replace(curLine.Offset, cursorOffset - curLine.Offset, indentation + " * ");
                                return indentation.Length + 3;
                            }

                            index = lineAboveText.IndexOf("*");
                            if (index > 0) {
                                string indentation = GetIndentation(d, lineNr - 1);
                                for (int i = indentation.Length; i < index; ++ i) {
                                    indentation += ' ';
                                }
                                d.Document.Replace(curLine.Offset, cursorOffset - curLine.Offset, indentation + "* ");
                                return indentation.Length + 2;
                            }
                        }
                    }
            #endif
                    return IndentLine(d, lineNr);
            }
            return 0;
        }
 // used for comment tag formater/inserter
 public override int FormatLine(IFormattableDocument d, int lineNr, int caretOffset, char charTyped)
 {
     try {
         if (charTyped == '>') {
             StringBuilder stringBuilder = new StringBuilder();
             int offset = Math.Min(caretOffset - 2, d.TextLength - 1);
             while (true) {
                 if (offset < 0) {
                     break;
                 }
                 char ch = d.GetCharAt(offset);
                 if (ch == '<') {
                     string reversedTag = stringBuilder.ToString().Trim();
                     if (!reversedTag.StartsWith("/") && !reversedTag.EndsWith("/")) {
                         bool validXml = true;
                         try {
                             XmlDocument doc = new XmlDocument();
                             doc.LoadXml(d.TextContent);
                         } catch (Exception) {
                             validXml = false;
                         }
                         // only insert the tag, if something is missing
                         if (!validXml) {
                             StringBuilder tag = new StringBuilder();
                             for (int i = reversedTag.Length - 1; i >= 0 && !Char.IsWhiteSpace(reversedTag[i]); --i) {
                                 tag.Append(reversedTag[i]);
                             }
                             string tagString = tag.ToString();
                             if (tagString.Length > 0) {
                                 d.Insert(caretOffset, "</" + tagString + ">");
                             }
                         }
                     }
                     break;
                 }
                 stringBuilder.Append(ch);
                 --offset;
             }
         }
     } catch (Exception e) { // Insanity check
         Debug.Assert(false, e.ToString());
     }
     return charTyped == '\n' ? IndentLine(d, lineNr) : 0;
 }
        // used for comment tag formater/inserter
        public override int FormatLine(IFormattableDocument d, int lineNr, int cursorOffset, char ch)
        {
            int curLineOffset, curLineLength;
            d.GetLineLengthInfo (lineNr, out curLineOffset, out curLineLength);

            if (ch != '\n' && ch != '>' && IsInsideStringOrComment (d, curLineOffset, cursorOffset))
                return 0;

            switch (ch) {
                case '>':
                    if (IsInsideDocumentationComment (d, curLineOffset, cursorOffset)) {
                        string curLineText  = d.GetLineAsString (lineNr);
                        int column = cursorOffset - curLineOffset;
                        int index = Math.Min (column - 1, curLineText.Length - 1);
                        if (curLineText [index] == '/')
                            break;

                        while (index >= 0 && curLineText [index] != '<')
                            --index;

                        if (index > 0) {
                            bool skipInsert = false;
                            for (int i = index; i < curLineText.Length && i < column; ++i) {
                                if (i < curLineText.Length && curLineText [i] == '/' && curLineText [i + 1] == '>')
                                    skipInsert = true;

                                if (curLineText [i] == '>')
                                    break;
                            }

                            if (skipInsert)
                                break;

                            StringBuilder commentBuilder = new StringBuilder ("");
                            for (int i = index; i < curLineText.Length && i < column && !Char.IsWhiteSpace (curLineText [i]); ++i)
                                commentBuilder.Append (curLineText [i]);

                            string tag = commentBuilder.ToString ().Trim ();
                            if (!tag.EndsWith (">"))
                                tag += ">";

                            if (!tag.StartsWith ("/"))
                                d.Insert (cursorOffset, "</" + tag.Substring (1));
                        }
                    }
                    break;
                case '}':
                case '{':
                    return IndentLine (d, lineNr);
                case '\n':
                    if (lineNr <= 0)
                        return IndentLine (d, lineNr);

                    if (d.AutoInsertCurlyBracket) {
                        string oldLineText = d.GetLineAsString (lineNr - 1);
                        if (oldLineText.EndsWith ("{") && NeedCurlyBracket (d.TextContent)) {
                            d.Insert (cursorOffset, "\n}");
                            IndentLine (d, lineNr + 1);
                        }
                    }

                    //string  lineAboveText = d.GetLineAsString (lineNr - 1);

            #if NON_PORTABLE_CODE
                    if (lineAbove.HighlightSpanStack != null && lineAbove.HighlightSpanStack.Count > 0) {
                        if (!((Span)lineAbove.HighlightSpanStack.Peek ()).StopEOL) {	// case for /* style comments
                            int index = lineAboveText.IndexOf ("/*");

                            if (index > 0) {
                                string indentation = GetIndentation (d, lineNr - 1);
                                for (int i = indentation.Length; i < index; ++ i)
                                    indentation += ' ';

                                d.Replace (curLine.Offset, cursorOffset - curLine.Offset, indentation + " * ");
                                return indentation.Length + 3;
                            }

                            index = lineAboveText.IndexOf ("*");
                            if (index > 0) {
                                string indentation = GetIndentation (d, lineNr - 1);
                                for (int i = indentation.Length; i < index; ++ i)
                                    indentation += ' ';

                                d.Replace (curLine.Offset, cursorOffset - curLine.Offset, indentation + "* ");
                                return indentation.Length + 2;
                            }
                        } else {
                            LineSegment nextLine = lineNr + 1 < d.TotalNumberOfLines ? d.GetLineSegment (lineNr + 1) : null;
                            string  nextLineText  = lineNr + 1 < d.TotalNumberOfLines ? d.GetText (nextLine.Offset, nextLine.Length) : "";

                            // don't handle // lines, because they're only one lined comments
                            int indexAbove = lineAboveText.IndexOf ("///");
                            int indexNext  = nextLineText.IndexOf ("///");

                            if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length)) {
                                string indentation = GetIndentation (d, lineNr - 1);
                                for (int i = indentation.Length; i < indexAbove; ++ i)
                                    indentation += ' ';

                                d.Replace (curLine.Offset, cursorOffset - curLine.Offset, indentation + "/// ");
                                return indentation.Length + 4;
                            }
                        }
                    }
            #endif
                    return IndentLine (d, lineNr);
            }
            return 0;
        }
        public override int FormatLine(IFormattableDocument textArea, int lineNr, int cursorOffset, char ch)
        {
            PropertyService propertyService = (PropertyService)ServiceManager.GetService(typeof(PropertyService));
            doCasing = propertyService.GetProperty("VBBinding.TextEditor.EnableCasing", true);
            doInsertion = propertyService.GetProperty("VBBinding.TextEditor.EnableEndConstructs", true);

            if (lineNr > 0) {
                //LineSegment curLine = textArea.Document.GetLineSegment(lineNr);
                //LineSegment lineAbove = lineNr > 0 ? textArea.Document.GetLineSegment(lineNr - 1) : null;

                //string curLineText = textArea.Document.GetText(curLine.Offset, curLine.Length);
                //string lineAboveText = textArea.Document.GetText(lineAbove.Offset, lineAbove.Length);
                string curLineText=textArea.GetLineAsString(lineNr).Trim();
                string lineAboveText=textArea.GetLineAsString(lineNr-1).Trim();

                if (ch == '\n' && lineAboveText != null) {
                    int undoCount = 1;

                    // remove comments
                    string texttoreplace = Regex.Replace(lineAboveText, "'.*$", "", RegexOptions.Singleline);
                    // remove string content
                    MatchCollection strmatches = Regex.Matches(texttoreplace, "\"[^\"]*?\"", RegexOptions.Singleline);
                    foreach (Match match in strmatches) {
                        texttoreplace = texttoreplace.Remove(match.Index, match.Length).Insert(match.Index, new String('-', match.Length));
                    }

                    if (doCasing) {
                        foreach (string keyword in keywords) {
                            string regex = "(?:\\W|^)(" + keyword + ")(?:\\W|$)";
                            MatchCollection matches = Regex.Matches(texttoreplace, regex, RegexOptions.IgnoreCase | RegexOptions.Singleline);
                            foreach (Match match in matches) {
                                textArea.ReplaceLine(lineNr-1 + match.Groups[1].Index, keyword);
                                ++undoCount;
                            }
                        }
                    }

                    if (doInsertion) {
                        foreach (VBStatement statement in statements) {
                            if (Regex.IsMatch(texttoreplace.Trim(), statement.StartRegex, RegexOptions.IgnoreCase)) {
                                string indentation = GetIndentation(textArea, lineNr - 1);
                                if (isEndStatementNeeded(textArea, statement, lineNr)) {
                                    //textArea.Insert(textArea.Caret.Offset, "\n" + indentation + statement.EndStatement);
                                    //++undoCount;
                                }
                                for (int i = 0; i < statement.IndentPlus; i++) {
                                    indentation += "\t";	//Tab.GetIndentationString(textArea.Document);
                                }

                                textArea.ReplaceLine(lineNr, indentation + curLineText.Trim());
                                //Is this automagic now?
                                //textArea.Document.UndoStack.UndoLast(undoCount + 1);
                                return indentation.Length;
                            }
                        }
                    }

                    if (IsInString(lineAboveText)) {
                        if (IsFinishedString(curLineText)) {
                            textArea.Insert(lineNr-1 + lineAboveText.Length,
                                                     "\" & _");
                            curLineText = textArea.GetLineAsString(lineNr);
                            textArea.Insert(lineNr, "\"");

                            if (IsElseConstruct(lineAboveText))
                                SmartIndentLine(textArea, lineNr - 1);
                            int result = SmartIndentLine(textArea, lineNr) + 1;
                            //textArea.UndoStack.UndoLast(undoCount + 3);
                            return result;
                        } else {
                            textArea.Insert(lineNr-1 + lineAboveText.Length,
                                                     "\"");
                            if (IsElseConstruct(lineAboveText))
                                SmartIndentLine(textArea, lineNr - 1);
                            int result = SmartIndentLine(textArea, lineNr);
                            //textArea.Document.UndoStack.UndoLast(undoCount + 2);
                            return result;
                        }
                    } else {
                        string indent = GetIndentation(textArea, lineNr - 1);
                        if (indent.Length > 0) {
                            //string newLineText = indent + TextUtilities.GetLineAsString(textArea.Document, lineNr).Trim();
                            string newLineText=indent + textArea.GetLineAsString(lineNr).Trim();
                            //curLine = textArea.GetLineAsString(lineNr);
                            textArea.ReplaceLine(lineNr, newLineText);
                            //++undoCount;
                        }
                        if (IsElseConstruct(lineAboveText))
                            SmartIndentLine(textArea, lineNr - 1);
                        //textArea.Document.UndoStack.UndoLast(undoCount);
                        return indent.Length;
                    }
                }
            }
            return 0;
        }
Exemple #5
0
        // used for comment tag formater/inserter
        public override int FormatLine(IFormattableDocument d, int lineNr, int cursorOffset, char ch)
        {
            switch (ch)
            {
            //case '}':
            //case '{':
            //	return d.FormattingStrategy.IndentLine (d, lineNr);
            case '\n':
                if (lineNr <= 0)
                {
                    return(IndentLine(d, lineNr));
                }

                if (d.AutoInsertCurlyBracket)
                {
                    string oldLineText = d.GetLineAsString(lineNr - 1);
                    if (oldLineText.EndsWith("{") && NeedCurlyBracket(d.TextContent))
                    {
                        d.Insert(cursorOffset, "\n}");
                        IndentLine(d, lineNr + 1);
                    }
                }

                string lineAboveText = d.GetLineAsString(lineNr - 1);


#if NON_PORTABLE_CODE
                if (lineAbove.HighlightSpanStack != null && lineAbove.HighlightSpanStack.Count > 0)
                {
                    if (!((Span)lineAbove.HighlightSpanStack.Peek()).StopEOL)                                   // case for /* style comments
                    {
                        int index = lineAboveText.IndexOf("/*");

                        if (index > 0)
                        {
                            string indentation = GetIndentation(d, lineNr - 1);
                            for (int i = indentation.Length; i < index; ++i)
                            {
                                indentation += ' ';
                            }
                            d.Document.Replace(curLine.Offset, cursorOffset - curLine.Offset, indentation + " * ");
                            return(indentation.Length + 3);
                        }

                        index = lineAboveText.IndexOf("*");
                        if (index > 0)
                        {
                            string indentation = GetIndentation(d, lineNr - 1);
                            for (int i = indentation.Length; i < index; ++i)
                            {
                                indentation += ' ';
                            }
                            d.Document.Replace(curLine.Offset, cursorOffset - curLine.Offset, indentation + "* ");
                            return(indentation.Length + 2);
                        }
                    }
                }
#endif
                return(IndentLine(d, lineNr));
            }
            return(0);
        }