35
35
public class TextEditor
37
37
IBookmarkBuffer bookmarkBuffer;
38
ICodeStyleOperations codeStyleOperations;
39
38
IEditableTextBuffer textBuffer;
40
39
IEncodedTextContent encodedTextContent;
41
IPositionable positionable;
42
40
ICompletionWidget completionWidget;
41
IClipboardHandler clipboardHandler;
42
string newLine = null;
44
43
// All line and column numbers are 1-based
46
45
public static TextEditor GetTextEditor (IBaseViewContent content)
52
51
TextEditor ed = new TextEditor ();
53
52
ed.textBuffer = tb;
54
53
ed.bookmarkBuffer = (IBookmarkBuffer) content.GetContent (typeof(IBookmarkBuffer));
55
ed.codeStyleOperations = (ICodeStyleOperations) content.GetContent (typeof(ICodeStyleOperations));
56
54
ed.encodedTextContent = (IEncodedTextContent) content.GetContent (typeof(IEncodedTextContent));
57
ed.positionable = (IPositionable) content.GetContent (typeof(IPositionable));
58
55
ed.completionWidget = (ICompletionWidget) content.GetContent (typeof(ICompletionWidget));
56
ed.clipboardHandler = (IClipboardHandler) content.GetContent (typeof(IClipboardHandler));
234
228
public void JumpTo (int line, int col)
236
if (positionable != null)
237
positionable.JumpTo (line, col);
230
if (textBuffer != null)
231
textBuffer.SetCaretTo (line, col);
239
233
int pos = GetPositionFromLineColumn (line, col);
240
234
CursorPosition = pos;
253
public void CommentCode ()
255
if (codeStyleOperations != null)
256
codeStyleOperations.CommentCode ();
259
public void UncommentCode ()
261
if (codeStyleOperations != null)
262
codeStyleOperations.UncommentCode ();
265
public void IndentSelection ()
267
if (codeStyleOperations != null)
268
codeStyleOperations.IndentSelection ();
271
public void UnIndentSelection ()
273
if (codeStyleOperations != null)
274
codeStyleOperations.UnIndentSelection ();
247
public string NewLine
250
if (newLine == null) {
251
int pos1 = GetPositionFromLineColumn (1, 1);
253
newLine = GetCharAt (pos1 - 1).ToString ();
255
char c1 = GetCharAt (pos1 - 2);
256
if (c1 == '\n' || c1 == '\r')
257
newLine = c1 + newLine;
260
newLine = Environment.NewLine;
277
266
public int GetLineLength (int line)
314
303
textBuffer.GetLineColumnFromPosition (offset, out openingLine, out col);
307
public static char GetMatchingBrace (char ch)
323
throw new System.ArgumentException (ch.ToString (), "ch");
325
public static bool IsBrace (char ch)
327
return ch == '(' || ch == ')' || ch == '{' || ch == '}' || ch == '[' || ch == ']';
329
public static bool IsOpenBrace (char ch)
331
return ch == '(' || ch == '{' || ch == '[';
334
public int SearchBracketForward (int offset, char openBracket, char closingBracket)
336
return MonoDevelop.Projects.Gui.Completion.TextUtilities.SearchBracketForward (completionWidget, offset, openBracket, closingBracket);
339
public int SearchBracketBackward (int offset, char openBracket, char closingBracket)
341
return MonoDevelop.Projects.Gui.Completion.TextUtilities.SearchBracketBackward (completionWidget, offset, openBracket, closingBracket);
344
public CodeCompletionContext CurrentCodeCompletionContext {
346
if (completionWidget == null)
348
return completionWidget.CreateCodeCompletionContext (this.CursorPosition);
352
public int SearchChar (int startPos, char searchChar)
354
bool isInString = false, isInChar = false;
355
bool isInLineComment = false, isInBlockComment = false;
357
for (int pos = startPos; pos < TextLength; pos++) {
358
char ch = GetCharAt (pos);
362
isInLineComment = false;
365
if (isInBlockComment) {
366
if (pos > 0 && GetCharAt (pos - 1) == '*')
367
isInBlockComment = false;
368
} else if (!isInString && !isInChar && pos + 1 < TextLength) {
369
char nextChar = GetCharAt (pos + 1);
371
isInLineComment = true;
372
if (!isInLineComment && nextChar == '*')
373
isInBlockComment = true;
377
if (!(isInChar || isInLineComment || isInBlockComment))
378
isInString = !isInString;
381
if (!(isInString || isInLineComment || isInBlockComment))
382
isInChar = !isInChar;
385
if (ch == searchChar) {
386
if (!(isInString || isInChar || isInLineComment || isInBlockComment))
395
public void GotoMatchingBrace ()
397
int offset = textBuffer.CursorPosition;
398
if (!IsBrace (textBuffer.GetCharAt (offset)))
401
if (offset >= 0 && IsBrace (textBuffer.GetCharAt (offset))) {
402
char open = textBuffer.GetCharAt (offset);
404
bool searchForward = IsOpenBrace (open);
406
close = GetMatchingBrace (open);
407
offset = SearchBracketForward (offset + 1, open, close);
409
open = GetMatchingBrace (open);
410
offset = SearchBracketBackward (offset - 1, open, close);
413
textBuffer.CursorPosition = offset;