📄 vbnetformattingstrategy.cs
字号:
textArea.Document.Replace(lineAbove.Offset + match.Index, match.Length, "End If");
else
textArea.Document.Replace(lineAbove.Offset + match.Index, match.Length, keyword);
++undoCount;
}
}
}
if (doInsertion)
{
if (Regex.IsMatch(texttoreplace.Trim(), @"^If .*[^_]$", RegexOptions.IgnoreCase)) {
if (false == Regex.IsMatch(texttoreplace, @"\bthen\b", RegexOptions.IgnoreCase)) {
textArea.Document.Insert(lineAbove.Offset + lineAbove.Length, " Then");
++undoCount;
texttoreplace += " Then";
}
}
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.Document.Insert(textArea.Caret.Offset, "\n" + indentation + statement.EndStatement);
++undoCount;
}
for (int i = 0; i < statement.IndentPlus; i++) {
indentation += Tab.GetIndentationString(textArea.Document);
}
textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + curLineText.Trim());
textArea.Document.UndoStack.UndoLast(undoCount + 1);
return indentation.Length;
}
}
}
if (IsInString(lineAboveText))
{
if (IsFinishedString(curLineText)) {
textArea.Document.Insert(lineAbove.Offset + lineAbove.Length,
"\" & _");
curLine = textArea.Document.GetLineSegment(lineNr);
textArea.Document.Insert(curLine.Offset, "\"");
if (IsElseConstruct(lineAboveText))
SmartIndentLine(textArea, lineNr - 1);
int result = SmartIndentLine(textArea, lineNr) + 1;
textArea.Document.UndoStack.UndoLast(undoCount + 3);
return result;
} else {
textArea.Document.Insert(lineAbove.Offset + lineAbove.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();
curLine = textArea.Document.GetLineSegment(lineNr);
textArea.Document.Replace(curLine.Offset, curLine.Length, newLineText);
++undoCount;
}
if (IsElseConstruct(lineAboveText))
SmartIndentLine(textArea, lineNr - 1);
textArea.Document.UndoStack.UndoLast(undoCount);
return indent.Length;
}
}
else if(ch == '>')
{
if (IsInsideDocumentationComment(textArea, curLine, cursorOffset))
{
curLineText = textArea.Document.GetText(curLine);
int column = textArea.Caret.Offset - curLine.Offset;
int index = Math.Min(column - 1, curLineText.Length - 1);
while (index > 0 && curLineText[index] != '<') {
--index;
if(curLineText[index] == '/')
return 0; // 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(textArea.Caret.Offset, "</" + tag.Substring(1));
}
}
}
}
}
return 0;
}
bool IsInsideDocumentationComment(TextArea textArea, LineSegment curLine, int cursorOffset)
{
for (int i = curLine.Offset; i < cursorOffset; ++i) {
char ch = textArea.Document.GetCharAt(i);
if (ch == '"') {
return false;
}
if (ch == '\'' && i + 2 < cursorOffset && textArea.Document.GetCharAt(i + 1) == '\'' && textArea.Document.GetCharAt(i + 2) == '\'')
{
return true;
}
}
return false;
}
/// <summary>
/// Gets the next member after the specified caret position.
/// </summary>
object GetMemberAfter(TextArea textArea, int caretLine)
{
string fileName = textArea.MotherTextEditorControl.FileName;
object nextElement = null;
if (fileName != null && fileName.Length > 0 ) {
ParseInformation parseInfo = ParserService.ParseFile(fileName, textArea.Document.TextContent);
if (parseInfo != null) {
ICompilationUnit currentCompilationUnit = parseInfo.BestCompilationUnit;
if (currentCompilationUnit != null) {
IClass currentClass = currentCompilationUnit.GetInnermostClass(caretLine, 0);
int nextElementLine = int.MaxValue;
if (currentClass == null) {
foreach (IClass c in currentCompilationUnit.Classes) {
if (c.Region.BeginLine < nextElementLine && c.Region.BeginLine > caretLine) {
nextElementLine = c.Region.BeginLine;
nextElement = c;
}
}
} else {
foreach (IClass c in currentClass.InnerClasses) {
if (c.Region.BeginLine < nextElementLine && c.Region.BeginLine > caretLine) {
nextElementLine = c.Region.BeginLine;
nextElement = c;
}
}
foreach (IMember m in currentClass.Methods) {
if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) {
nextElementLine = m.Region.BeginLine;
nextElement = m;
}
}
foreach (IMember m in currentClass.Properties) {
if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) {
nextElementLine = m.Region.BeginLine;
nextElement = m;
}
}
foreach (IMember m in currentClass.Fields) {
if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) {
nextElementLine = m.Region.BeginLine;
nextElement = m;
}
}
foreach (IMember m in currentClass.Events) {
if (m.Region.BeginLine < nextElementLine && m.Region.BeginLine > caretLine) {
nextElementLine = m.Region.BeginLine;
nextElement = m;
}
}
}
}
}
}
return nextElement;
}
bool IsInString(string start)
{
bool inString = false;
for (int i = 0; i < start.Length; i++) {
if (start[i] == '"')
inString = !inString;
if (!inString && start[i] == '\'')
return false;
}
return inString;
}
bool IsFinishedString(string end)
{
bool inString = true;
for (int i = 0; i < end.Length; i++) {
if (end[i] == '"')
inString = !inString;
if (!inString && end[i] == '\'')
break;
}
return !inString;
}
bool isEndStatementNeeded(TextArea textArea, VBStatement statement, int lineNr)
{
int count = 0;
for (int i = 0; i < textArea.Document.TotalNumberOfLines; i++) {
LineSegment line = textArea.Document.GetLineSegment(i);
string lineText = textArea.Document.GetText(line.Offset, line.Length).Trim();
if (lineText.StartsWith("'")) {
continue;
}
if (Regex.IsMatch(lineText, statement.StartRegex, RegexOptions.IgnoreCase)) {
count++;
} else if (Regex.IsMatch(lineText, statement.EndRegex, RegexOptions.IgnoreCase)) {
count--;
}
}
return count > 0;
}
class VBStatement
{
public string StartRegex = "";
public string EndRegex = "";
public string EndStatement = "";
public int IndentPlus = 0;
public VBStatement()
{
}
public VBStatement(string startRegex, string endRegex, string endStatement, int indentPlus)
{
StartRegex = startRegex;
EndRegex = endRegex;
EndStatement = endStatement;
IndentPlus = indentPlus;
}
}
#region SearchBracket
public override int SearchBracketBackward(IDocument document, int offset, char openBracket, char closingBracket)
{
bool inString = false;
char ch;
int brackets = -1;
for (int i = offset; i > 0; --i) {
ch = document.GetCharAt(i);
if (ch == openBracket && !inString) {
++brackets;
if (brackets == 0) return i;
} else if (ch == closingBracket && !inString) {
--brackets;
} else if (ch == '"') {
inString = !inString;
} else if (ch == '\n') {
int lineStart = ScanLineStart(document, i);
if (lineStart >= 0) { // line could have a comment
inString = false;
for (int j = lineStart; j < i; ++j) {
ch = document.GetCharAt(j);
if (ch == '"') inString = !inString;
if (ch == '\'' && !inString) {
// comment found!
// Skip searching in the comment:
i = j;
break;
}
}
}
inString = false;
}
}
return -1;
}
static int ScanLineStart(IDocument document, int offset)
{
bool hasComment = false;
for (int i = offset - 1; i > 0; --i) {
char ch = document.GetCharAt(i);
if (ch == '\n') {
if (!hasComment) return -1;
return i + 1;
} else if (ch == '\'') {
hasComment = true;
}
}
return 0;
}
public override int SearchBracketForward(IDocument document, int offset, char openBracket, char closingBracket)
{
bool inString = false;
bool inComment = false;
int brackets = 1;
for (int i = offset; i < document.TextLength; ++i) {
char ch = document.GetCharAt(i);
if (ch == '\n') {
inString = false;
inComment = false;
}
if (inComment) continue;
if (ch == '"') inString = !inString;
if (inString) continue;
if (ch == '\'') {
inComment = true;
} else if (ch == openBracket) {
++brackets;
} else if (ch == closingBracket) {
--brackets;
if (brackets == 0) return i;
}
}
return -1;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -