18 using System.Collections.Generic;
20 using System.Text.RegularExpressions;
21 using Microsoft.VisualStudio.Package;
22 using Microsoft.VisualStudio.TextManager.Interop;
33 static private Regex matchEndOfStatement;
34 private static Regex matchBraceStart;
37 matchEndOfStatement =
new Regex(
@";[\s\r\n]*([\}]?)");
38 matchBraceStart =
new Regex(
@"\{[\s\r\n]*");
41 public static List<EditSpan>
ReformatCode(IVsTextLines pBuffer, TextSpan span,
int tabSize)
43 string filePath = FilePathUtilities.GetFilePath(pBuffer);
47 List<EditSpan> changeList =
new List<EditSpan>();
49 pBuffer.GetLineCount(out nbLines);
54 pBuffer.GetLastLineIndex(out lastLine, out lastLineIndex);
55 pBuffer.GetLineText(0, 0, lastLine, lastLineIndex, out codeToFormat);
57 NShaderScanner shaderScanner = NShaderScannerFactory.GetShaderScanner(filePath);
58 Scanner lexer = shaderScanner.Lexer;
59 lexer.SetSource(codeToFormat, 0);
63 pBuffer.GetPositionOfLineIndex(span.iStartLine, span.iStartIndex, out spanStart);
64 pBuffer.GetPositionOfLineIndex(span.iEndLine, span.iEndIndex, out spanEnd);
70 List<int> brackets =
new List<int>();
71 List<int> delimiters =
new List<int>();
75 int levelParenthesis = 0;
80 case ShaderToken.LEFT_PARENTHESIS:
83 case ShaderToken.RIGHT_PARENTHESIS:
85 if ( levelParenthesis < 0 )
90 case ShaderToken.LEFT_BRACKET:
92 if (codeToFormat[start] ==
'{' && start >= spanStart && end <= spanEnd)
94 Match match = matchBraceStart.Match(codeToFormat, start);
97 StringBuilder codeFormatted =
new StringBuilder();
98 codeFormatted.Append(
"{\r\n");
99 int levelToIndentNext = level;
100 if (match.Groups.Count == 2)
102 string matchStr = match.Groups[1].Value;
105 for (
int i = 0; i < levelToIndentNext; i++)
107 for (
int j = 0; j < tabSize; j++)
109 codeFormatted.Append(
' ');
112 if (match.Groups.Count == 2)
114 codeFormatted.Append(
"}\r\n");
117 TextSpan editTextSpan =
new TextSpan();
119 pBuffer.GetLineIndexOfPosition(start,
120 out editTextSpan.iStartLine,
121 out editTextSpan.iStartIndex);
122 pBuffer.GetLineIndexOfPosition(startCopy + match.Index + match.Length,
123 out editTextSpan.iEndLine,
124 out editTextSpan.iEndIndex);
126 changeList.Add(
new EditSpan(editTextSpan, codeFormatted.ToString()));
129 case ShaderToken.RIGHT_BRACKET:
137 case ShaderToken.DELIMITER:
138 if (codeToFormat[start] ==
';' && start >= spanStart && end <= spanEnd && levelParenthesis == 0)
140 Match match = matchEndOfStatement.Match(codeToFormat, start);
142 StringBuilder codeFormatted =
new StringBuilder();
143 codeFormatted.Append(
";\r\n");
144 int levelToIndentNext = level;
145 bool isBracketFound = (match.Groups.Count == 2 && match.Groups[1].Value ==
"}");
148 string matchStr = match.Groups[1].Value;
151 for (
int i = 0; i < levelToIndentNext; i++)
153 for (
int j = 0; j < tabSize; j++)
155 codeFormatted.Append(
' ');
160 codeFormatted.Append(
"}\r\n");
163 TextSpan editTextSpan =
new TextSpan();
165 pBuffer.GetLineIndexOfPosition(start,
166 out editTextSpan.iStartLine,
167 out editTextSpan.iStartIndex);
168 pBuffer.GetLineIndexOfPosition(startCopy + match.Index + match.Length,
169 out editTextSpan.iEndLine,
170 out editTextSpan.iEndIndex);
172 changeList.Add(
new EditSpan(editTextSpan, codeFormatted.ToString()));
176 token = (
ShaderToken) lexer.GetNext(ref state, out start, out end);
int GetNext(ref int state, out int start, out int end)