private void FormatLineInternal(TextArea textArea, int lineNr, int cursorOffset, char ch) { DocumentLine curLine = textArea.Document.GetLineByNumber(lineNr); DocumentLine lineAbove = lineNr > 1 ? textArea.Document.GetLineByNumber(lineNr - 1) : null; string terminator = TextUtilities.GetLineTerminator(textArea.Document, lineNr); string curLineText; if (ch != '\n' && ch != '>') { if (IsInsideStringOrComment(textArea, curLine, cursorOffset)) { return; } } switch (ch) { case '>': if (IsInsideDocumentationComment(textArea, curLine, cursorOffset)) { curLineText = textArea.Document.GetText(curLine); int column = cursorOffset - curLine.Offset; int index = Math.Min(column - 1, curLineText.Length - 1); while (index >= 0 && curLineText[index] != '<') { --index; if (curLineText[index] == '/') { return; // the tag was an end tag or already } } if (index > 0) { 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("/")) { textArea.Document.Insert(cursorOffset, "</" + tag.Substring(1)); } } } break; case ':': case ')': case ']': case '}': case '{': if (textArea.IndentationStrategy != null) { textArea.IndentationStrategy.IndentLine(textArea, curLine); } break; case '\n': string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); //// curLine might have some text which should be added to indentation curLineText = textArea.Document.GetText(curLine); IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter; bool isInMultilineComment = false; bool isInMultilineString = false; if (highlighter != null && lineAbove != null) { var spanStack = highlighter.GetSpanColorNamesFromLineStart(lineNr); isInMultilineComment = spanStack.Contains(HighlightingKnownNames.Comment); isInMultilineString = spanStack.Contains(HighlightingKnownNames.String); } bool isInNormalCode = !(isInMultilineComment || isInMultilineString); if (lineAbove != null && isInMultilineComment) { string lineAboveTextTrimmed = lineAboveText.TrimStart(); if (lineAboveTextTrimmed.StartsWith("/*", StringComparison.Ordinal)) { textArea.Document.Insert(cursorOffset, " * "); return; } if (lineAboveTextTrimmed.StartsWith("*", StringComparison.Ordinal)) { textArea.Document.Insert(cursorOffset, "* "); return; } } if (lineAbove != null && isInNormalCode) { DocumentLine nextLine = lineNr + 1 <= textArea.Document.LineCount ? textArea.Document.GetLineByNumber(lineNr + 1) : null; string nextLineText = (nextLine != null) ? textArea.Document.GetText(nextLine) : ""; int indexAbove = lineAboveText.IndexOf("///"); int indexNext = nextLineText.IndexOf("///"); if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length)) { textArea.Document.Insert(cursorOffset, "/// "); return; } if (IsInNonVerbatimString(lineAboveText, curLineText)) { textArea.Document.Insert(cursorOffset, "\""); textArea.Document.Insert(lineAbove.Offset + lineAbove.Length, "\" +"); } } if (/*textArea.Options.AutoInsertBlockEnd &&*/ lineAbove != null && isInNormalCode) { string oldLineText = textArea.Document.GetText(lineAbove); if (oldLineText.EndsWith("{")) { if (NeedCurlyBracket(textArea.Document.Text)) { int insertionPoint = curLine.Offset + curLine.Length; textArea.Document.Insert(insertionPoint, terminator + "}"); if (textArea.IndentationStrategy != null) { textArea.IndentationStrategy.IndentLine(textArea, textArea.Document.GetLineByNumber(lineNr + 1)); } textArea.Caret.Offset = insertionPoint; } } } return; } }
/// <summary> /// Determines whether current line is inside a string. /// </summary> /// <param name="highlighter">The syntax highlighter.</param> /// <param name="lineNumber">The line number.</param> /// <returns> /// <see langword="true"/> if the specified line is inside a string; otherwise, /// <see langword="false"/>. /// </returns> public static bool IsLineStartInsideString(this IHighlighter highlighter, int lineNumber) { return(highlighter.GetSpanColorNamesFromLineStart(lineNumber).Contains(String)); }
private void FormatLineInternal(TextArea textArea, int lineNr, int cursorOffset, char ch) { DocumentLine curLine = textArea.Document.GetLineByNumber(lineNr); DocumentLine lineAbove = lineNr > 1 ? textArea.Document.GetLineByNumber(lineNr - 1) : null; string terminator = TextUtilities.GetLineTerminator(textArea.Document, lineNr); string curLineText; // /// local string for curLine segment //if (ch == '/') //{ // curLineText = curLine.Text; // string lineAboveText = lineAbove == null ? "" : lineAbove.Text; // if (curLineText != null && curLineText.EndsWith("///") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("///"))) // { // string indentation = DocumentUtilitites.GetWhitespaceAfter(textArea.Document, curLine.Offset); // object member = GetMemberAfter(textArea, lineNr); // if (member != null) // { // StringBuilder sb = new StringBuilder(); // sb.Append(" <summary>"); // sb.Append(terminator); // sb.Append(indentation); // sb.Append("/// "); // sb.Append(terminator); // sb.Append(indentation); // sb.Append("/// </summary>"); // if (member is IMethod) // { // IMethod method = (IMethod)member; // if (method.Parameters != null && method.Parameters.Count > 0) // { // for (int i = 0; i < method.Parameters.Count; ++i) // { // sb.Append(terminator); // sb.Append(indentation); // sb.Append("/// <param name=\""); // sb.Append(method.Parameters[i].Name); // sb.Append("\"></param>"); // } // } // if (method.ReturnType != null && !method.IsConstructor && method.ReturnType.FullyQualifiedName != "System.Void") // { // sb.Append(terminator); // sb.Append(indentation); // sb.Append("/// <returns></returns>"); // } // } // textArea.Document.Insert(cursorOffset, sb.ToString()); // textArea.Caret.Offset = cursorOffset + indentation.Length + "/// ".Length + " <summary>".Length + terminator.Length; // } // } // return; //} if (ch != '\n' && ch != '>') { if (IsInsideStringOrComment(textArea, curLine, cursorOffset)) { return; } } switch (ch) { case '>': if (IsInsideDocumentationComment(textArea, curLine, cursorOffset)) { curLineText = textArea.Document.GetText(curLine); int column = cursorOffset - curLine.Offset; int index = Math.Min(column - 1, curLineText.Length - 1); while (index >= 0 && curLineText[index] != '<') { --index; if (curLineText[index] == '/') { return; // the tag was an end tag or already } } if (index > 0) { 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("/")) { textArea.Document.Insert(cursorOffset, "</" + tag.Substring(1)); } } } break; case ':': case ')': case ']': case '}': case '{': if (textArea.IndentationStrategy != null) { textArea.IndentationStrategy.IndentLine(textArea, curLine); } break; case '\n': string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); //// curLine might have some text which should be added to indentation curLineText = textArea.Document.GetText(curLine); if (lineAbove != null && textArea.Document.GetText(lineAbove).Trim().StartsWith("#region") && NeedEndregion(textArea.Document)) { textArea.Document.Insert(cursorOffset, "#endregion"); return; } IHighlighter highlighter = textArea.GetService(typeof(IHighlighter)) as IHighlighter; bool isInMultilineComment = false; bool isInMultilineString = false; if (highlighter != null && lineAbove != null) { var spanStack = highlighter.GetSpanColorNamesFromLineStart(lineNr); isInMultilineComment = spanStack.Contains(HighlightingKnownNames.Comment); isInMultilineString = spanStack.Contains(HighlightingKnownNames.String); } bool isInNormalCode = !(isInMultilineComment || isInMultilineString); if (lineAbove != null && isInMultilineComment) { string lineAboveTextTrimmed = lineAboveText.TrimStart(); if (lineAboveTextTrimmed.StartsWith("/*", StringComparison.Ordinal)) { textArea.Document.Insert(cursorOffset, " * "); return; } if (lineAboveTextTrimmed.StartsWith("*", StringComparison.Ordinal)) { textArea.Document.Insert(cursorOffset, "* "); return; } } if (lineAbove != null && isInNormalCode) { DocumentLine nextLine = lineNr + 1 <= textArea.Document.LineCount ? textArea.Document.GetLineByNumber(lineNr + 1) : null; string nextLineText = (nextLine != null) ? textArea.Document.GetText(nextLine) : ""; int indexAbove = lineAboveText.IndexOf("///"); int indexNext = nextLineText.IndexOf("///"); if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length)) { textArea.Document.Insert(cursorOffset, "/// "); return; } if (IsInNonVerbatimString(lineAboveText, curLineText)) { textArea.Document.Insert(cursorOffset, "\""); textArea.Document.Insert(lineAbove.Offset + lineAbove.Length, "\" +"); } } if (/*textArea.Options.AutoInsertBlockEnd &&*/ lineAbove != null && isInNormalCode) { string oldLineText = textArea.Document.GetText(lineAbove); if (oldLineText.EndsWith("{")) { if (NeedCurlyBracket(textArea.Document.Text)) { int insertionPoint = curLine.Offset + curLine.Length; textArea.Document.Insert(insertionPoint, terminator + "}"); if (textArea.IndentationStrategy != null) { textArea.IndentationStrategy.IndentLine(textArea, textArea.Document.GetLineByNumber(lineNr + 1)); } textArea.Caret.Offset = insertionPoint; } } } return; } }