Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NShaderSource.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.Linq;
3 
4 using Microsoft.VisualStudio.Package;
5 using Microsoft.VisualStudio.TextManager.Interop;
6 
7 namespace NShader
8 {
9  public class NShaderSource : Source
10  {
11  public NShaderSource(LanguageService service, IVsTextLines textLines, Colorizer colorizer) : base(service, textLines, colorizer)
12  {
13  }
14 
15  private void DoFormatting(EditArray mgr, TextSpan span)
16  {
17  // Make sure there is one space after every comma unless followed
18  // by a tab or comma is at end of line.
19  IVsTextLines pBuffer = GetTextLines();
20  if (pBuffer != null)
21  {
22 // List<EditSpan> changeList = new List<EditSpan>();
23 
24  // BETA DISABLED
25  List<EditSpan> changeList = NShaderFormatHelper.ReformatCode(pBuffer, span, LanguageService.GetLanguagePreferences().TabSize);
26  foreach (EditSpan editSpan in changeList)
27  {
28  // Add edit operation
29  mgr.Add(editSpan);
30  }
31  // Apply all edits
32  mgr.ApplyEdits();
33  }
34  }
35 
36  public override void ReformatSpan(EditArray mgr, TextSpan span)
37  {
38  string description = "Reformat code";
39  CompoundAction ca = new CompoundAction(this, description);
40  using (ca)
41  {
42  ca.FlushEditActions(); // Flush any pending edits
43  DoFormatting(mgr, span); // Format the span
44  }
45  }
46 
47  #region Commenting, Greetings to from http://blog.280z28.org/archives/2008/10/19/
48 
49  public override TextSpan CommentSpan(TextSpan span)
50  {
51  TextSpan result = span;
52  CommentInfo commentInfo = GetCommentFormat();
53 
54  using (new CompoundAction(this, "Comment this selection"))
55  {
56  /*
57  * Use line comments if:
58  * UseLineComments is true
59  * AND LineStart is not null or empty
60  * AND one of the following is true:
61  *
62  * 1. there is no selected text
63  * 2. on the line where the selection starts, there is only whitespace up to the selection start point
64  * AND on the line where the selection ends, there is only whitespace up to the selection end point,
65  * OR there is only whitespace from the selection end point to the end of the line
66  *
67  * Use block comments if:
68  * We are not using line comments
69  * AND some text is selected
70  * AND BlockStart is not null or empty
71  * AND BlockEnd is not null or empty
72  */
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))
79  )))
80  {
81  result = CommentLines(span, commentInfo.LineStart);
82  }
83  else if (
84  TextSpanHelper.IsPositive(span)
85  && !string.IsNullOrEmpty(commentInfo.BlockStart)
86  && !string.IsNullOrEmpty(commentInfo.BlockEnd)
87  )
88  {
89  result = CommentBlock(span, commentInfo.BlockStart, commentInfo.BlockEnd);
90  }
91  }
92  return result;
93  }
94 
95  public override TextSpan CommentLines(TextSpan span, string lineComment)
96  {
97  /*
98  * Rules for line comments:
99  * Make sure line comments are indented as far as possible, skipping empty lines as necessary
100  * Don't comment N+1 lines when only N lines were selected my clicking in the left margin
101  */
102  if (span.iEndLine > span.iStartLine && span.iEndIndex == 0)
103  span.iEndLine--;
104 
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))
108  .Min();
109 
110  //comment each line
111  for (int line = span.iStartLine; line <= span.iEndLine; line++)
112  {
113  if (GetLine(line).Trim().Length > 0)
114  SetText(line, minindex, line, minindex, lineComment);
115  }
116 
117  span.iStartIndex = 0;
118  span.iEndIndex = GetLineLength(span.iEndLine);
119 
120  return span;
121  }
122 
123  public override TextSpan CommentBlock(TextSpan span, string blockStart, string blockEnd)
124  {
125  //sp. case no selection
126  if (span.iStartIndex == span.iEndIndex &&
127  span.iStartLine == span.iEndLine)
128  {
129  span.iStartIndex = ScanToNonWhitespaceChar(span.iStartLine);
130  span.iEndIndex = GetLineLength(span.iEndLine);
131  }
132  //sp. case partial selection on single line
133  if (span.iStartLine == span.iEndLine)
134  {
135  span.iEndIndex += blockStart.Length;
136  }
137  //add start comment
138  SetText(span.iStartLine, span.iStartIndex, span.iStartLine, span.iStartIndex, blockStart);
139  //add end comment
140  SetText(span.iEndLine, span.iEndIndex, span.iEndLine, span.iEndIndex, blockEnd);
141  span.iEndIndex += blockEnd.Length;
142  return span;
143  }
144 
145  public override TextSpan UncommentSpan(TextSpan span)
146  {
147  CommentInfo commentInfo = GetCommentFormat();
148 
149  using (new CompoundAction(this, "Uncomment this selection"))
150  {
151  // special case: empty span
152  if (TextSpanHelper.IsEmpty(span))
153  {
154  if (commentInfo.UseLineComments)
155  span = UncommentLines(span, commentInfo.LineStart);
156  return span;
157  }
158 
159  string textblock = GetText(span).Trim();
160 
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))
166  {
167  TrimSpan(ref span);
168  span = UncommentBlock(span, commentInfo.BlockStart, commentInfo.BlockEnd);
169  }
170  else if (commentInfo.UseLineComments && !string.IsNullOrEmpty(commentInfo.LineStart))
171  {
172  span = UncommentLines(span, commentInfo.LineStart);
173  }
174  }
175  return span;
176  }
177 
178  public override TextSpan UncommentLines(TextSpan span, string lineComment)
179  {
180  if (span.iEndLine > span.iStartLine && span.iEndIndex == 0)
181  span.iEndLine--;
182 
183  // Remove line comments
184  int clen = lineComment.Length;
185  for (int line = span.iStartLine; line <= span.iEndLine; line++)
186  {
187  int i = ScanToNonWhitespaceChar(line);
188  string text = GetLine(line);
189  if ((text.Length > i + clen) && text.Substring(i, clen) == lineComment)
190  {
191  SetText(line, i, line, i + clen, ""); // remove line comment.
192  }
193  }
194 
195  span.iStartIndex = 0;
196  span.iEndIndex = GetLineLength(span.iEndLine);
197  return span;
198  }
199 
200  public override TextSpan UncommentBlock(TextSpan span, string blockStart, string blockEnd)
201  {
202 
203  int startLen = GetLineLength(span.iStartLine);
204  int endLen = GetLineLength(span.iEndLine);
205 
206  TextSpan result = span;
207 
208  //sp. case no selection, try and uncomment the current line.
209  if (span.iStartIndex == span.iEndIndex &&
210  span.iStartLine == span.iEndLine)
211  {
212  span.iStartIndex = ScanToNonWhitespaceChar(span.iStartLine);
213  span.iEndIndex = GetLineLength(span.iEndLine);
214  }
215 
216  // Check that comment start and end blocks are possible.
217  if (span.iStartIndex + blockStart.Length <= startLen && span.iEndIndex - blockStart.Length >= 0)
218  {
219  string startText = GetText(span.iStartLine, span.iStartIndex, span.iStartLine, span.iStartIndex + blockStart.Length);
220 
221  if (startText == blockStart)
222  {
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)
230  {
231  //yes, block comment selected; remove it
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;
237  result = span;
238  }
239  }
240  }
241 
242  return result;
243  }
244 
245  #endregion
246 
247  }
248 }
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)