1 using System.Collections.Generic;
4 using Microsoft.VisualStudio.Package;
5 using Microsoft.VisualStudio.TextManager.Interop;
15 private void DoFormatting(EditArray mgr, TextSpan span)
19 IVsTextLines pBuffer = GetTextLines();
25 List<EditSpan> changeList = NShaderFormatHelper.ReformatCode(pBuffer, span, LanguageService.GetLanguagePreferences().TabSize);
26 foreach (EditSpan editSpan
in changeList)
38 string description =
"Reformat code";
39 CompoundAction ca =
new CompoundAction(
this, description);
42 ca.FlushEditActions();
43 DoFormatting(mgr, span);
47 #region Commenting, Greetings to from http://blog.280z28.org/archives/2008/10/19/
51 TextSpan result = span;
52 CommentInfo commentInfo = GetCommentFormat();
54 using (
new CompoundAction(
this,
"Comment this selection"))
73 if (commentInfo.UseLineComments
74 && !
string.IsNullOrEmpty(commentInfo.LineStart)
75 && (TextSpanHelper.IsEmpty(span) ||
76 ((GetText(span.iStartLine, 0, span.iStartLine, span.iStartIndex).Trim().Length == 0)
77 && ((GetText(span.iEndLine, 0, span.iEndLine, span.iEndIndex).Trim().Length == 0)
78 || (GetText(span.iEndLine, span.iEndIndex, span.iEndLine, GetLineLength(span.iEndLine)).Trim().Length == 0))
84 TextSpanHelper.IsPositive(span)
85 && !string.IsNullOrEmpty(commentInfo.BlockStart)
86 && !
string.IsNullOrEmpty(commentInfo.BlockEnd)
89 result =
CommentBlock(span, commentInfo.BlockStart, commentInfo.BlockEnd);
95 public override TextSpan
CommentLines(TextSpan span,
string lineComment)
102 if (span.iEndLine > span.iStartLine && span.iEndIndex == 0)
105 int minindex = (from i in Enumerable.Range(span.iStartLine, span.iEndLine - span.iStartLine + 1)
106 where GetLine(i).Trim().Length > 0
107 select ScanToNonWhitespaceChar(i))
111 for (
int line = span.iStartLine; line <= span.iEndLine; line++)
113 if (GetLine(line).Trim().Length > 0)
114 SetText(line, minindex, line, minindex, lineComment);
117 span.iStartIndex = 0;
118 span.iEndIndex = GetLineLength(span.iEndLine);
123 public override TextSpan
CommentBlock(TextSpan span,
string blockStart,
string blockEnd)
126 if (span.iStartIndex == span.iEndIndex &&
127 span.iStartLine == span.iEndLine)
129 span.iStartIndex = ScanToNonWhitespaceChar(span.iStartLine);
130 span.iEndIndex = GetLineLength(span.iEndLine);
133 if (span.iStartLine == span.iEndLine)
135 span.iEndIndex += blockStart.Length;
138 SetText(span.iStartLine, span.iStartIndex, span.iStartLine, span.iStartIndex, blockStart);
140 SetText(span.iEndLine, span.iEndIndex, span.iEndLine, span.iEndIndex, blockEnd);
141 span.iEndIndex += blockEnd.Length;
147 CommentInfo commentInfo = GetCommentFormat();
149 using (
new CompoundAction(
this,
"Uncomment this selection"))
152 if (TextSpanHelper.IsEmpty(span))
154 if (commentInfo.UseLineComments)
159 string textblock = GetText(span).Trim();
161 if (!
string.IsNullOrEmpty(commentInfo.BlockStart)
162 && !string.IsNullOrEmpty(commentInfo.BlockEnd)
163 && textblock.Length >= commentInfo.BlockStart.Length + commentInfo.BlockEnd.Length
164 && textblock.StartsWith(commentInfo.BlockStart)
165 && textblock.EndsWith(commentInfo.BlockEnd))
168 span =
UncommentBlock(span, commentInfo.BlockStart, commentInfo.BlockEnd);
170 else if (commentInfo.UseLineComments && !
string.IsNullOrEmpty(commentInfo.LineStart))
180 if (span.iEndLine > span.iStartLine && span.iEndIndex == 0)
184 int clen = lineComment.Length;
185 for (
int line = span.iStartLine; line <= span.iEndLine; line++)
187 int i = ScanToNonWhitespaceChar(line);
188 string text = GetLine(line);
189 if ((text.Length > i + clen) && text.Substring(i, clen) == lineComment)
191 SetText(line, i, line, i + clen,
"");
195 span.iStartIndex = 0;
196 span.iEndIndex = GetLineLength(span.iEndLine);
200 public override TextSpan
UncommentBlock(TextSpan span,
string blockStart,
string blockEnd)
203 int startLen = GetLineLength(span.iStartLine);
204 int endLen = GetLineLength(span.iEndLine);
206 TextSpan result = span;
209 if (span.iStartIndex == span.iEndIndex &&
210 span.iStartLine == span.iEndLine)
212 span.iStartIndex = ScanToNonWhitespaceChar(span.iStartLine);
213 span.iEndIndex = GetLineLength(span.iEndLine);
217 if (span.iStartIndex + blockStart.Length <= startLen && span.iEndIndex - blockStart.Length >= 0)
219 string startText = GetText(span.iStartLine, span.iStartIndex, span.iStartLine, span.iStartIndex + blockStart.Length);
221 if (startText == blockStart)
223 string endText = null;
224 TextSpan linespan = span;
225 linespan.iStartLine = linespan.iEndLine;
226 linespan.iStartIndex = linespan.iEndIndex - blockEnd.Length;
227 System.Diagnostics.Debug.Assert(TextSpanHelper.IsPositive(linespan));
228 endText = GetText(linespan);
229 if (endText == blockEnd)
232 SetText(linespan.iStartLine, linespan.iStartIndex, linespan.iEndLine, linespan.iEndIndex, null);
233 SetText(span.iStartLine, span.iStartIndex, span.iStartLine, span.iStartIndex + blockStart.Length, null);
234 span.iEndIndex -= blockEnd.Length;
235 if (span.iStartLine == span.iEndLine)
236 span.iEndIndex -= blockStart.Length;
override TextSpan CommentSpan(TextSpan span)
override TextSpan CommentBlock(TextSpan span, string blockStart, string blockEnd)
NShaderSource(LanguageService service, IVsTextLines textLines, Colorizer colorizer)
override TextSpan UncommentBlock(TextSpan span, string blockStart, string blockEnd)
override TextSpan CommentLines(TextSpan span, string lineComment)
override TextSpan UncommentSpan(TextSpan span)
override TextSpan UncommentLines(TextSpan span, string lineComment)
override void ReformatSpan(EditArray mgr, TextSpan span)