~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui/TextEditor.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
        public class TextEditor
36
36
        {
37
37
                IBookmarkBuffer bookmarkBuffer;
38
 
                ICodeStyleOperations codeStyleOperations;
39
38
                IEditableTextBuffer textBuffer;
40
39
                IEncodedTextContent encodedTextContent;
41
 
                IPositionable positionable;
42
40
                ICompletionWidget completionWidget;
43
 
                
 
41
                IClipboardHandler clipboardHandler;
 
42
                string newLine = null;
44
43
                // All line and column numbers are 1-based
45
44
                
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));
59
57
                        return ed;
60
58
                }
61
59
                
63
61
                        get { return bookmarkBuffer != null; }
64
62
                }
65
63
                
66
 
                public bool SupportsCodeStyleOperations {
67
 
                        get { return codeStyleOperations != null; }
68
 
                }
69
 
                
70
64
                public void SetBookmarked (int position, bool mark)
71
65
                {
72
66
                        if (bookmarkBuffer != null)
100
94
                }
101
95
                
102
96
                public IClipboardHandler ClipboardHandler {
103
 
                        get { return textBuffer.ClipboardHandler; }
 
97
                        get { return clipboardHandler; }
104
98
                }
105
99
                
106
100
                public void Undo()
128
122
                        set { textBuffer.SelectedText = value; }
129
123
                }
130
124
                
131
 
                public event TextChangedEventHandler TextChanged {
 
125
                public event EventHandler<TextChangedEventArgs> TextChanged {
132
126
                        add { textBuffer.TextChanged += value; }
133
127
                        remove { textBuffer.TextChanged -= value; }
134
128
                }
233
227
                
234
228
                public void JumpTo (int line, int col)
235
229
                {
236
 
                        if (positionable != null)
237
 
                                positionable.JumpTo (line, col);
 
230
                        if (textBuffer != null)
 
231
                                textBuffer.SetCaretTo (line, col);
238
232
                        else {
239
233
                                int pos = GetPositionFromLineColumn (line, col);
240
234
                                CursorPosition = pos;
249
243
                                        return null;
250
244
                        }
251
245
                }
252
 
 
253
 
                public void CommentCode ()
254
 
                {
255
 
                        if (codeStyleOperations != null)
256
 
                                codeStyleOperations.CommentCode ();
257
 
                }
258
 
                
259
 
                public void UncommentCode ()
260
 
                {
261
 
                        if (codeStyleOperations != null)
262
 
                                codeStyleOperations.UncommentCode ();
263
 
                }
264
 
                
265
 
                public void IndentSelection ()
266
 
                {
267
 
                        if (codeStyleOperations != null)
268
 
                                codeStyleOperations.IndentSelection ();
269
 
                }
270
 
                
271
 
                public void UnIndentSelection ()
272
 
                {
273
 
                        if (codeStyleOperations != null)
274
 
                                codeStyleOperations.UnIndentSelection ();
 
246
                
 
247
                public string NewLine
 
248
                {
 
249
                        get {
 
250
                                if (newLine == null) {
 
251
                                        int pos1 = GetPositionFromLineColumn (1, 1);
 
252
                                        if (pos1 > 1)
 
253
                                                newLine = GetCharAt (pos1 - 1).ToString ();
 
254
                                        if (pos1 > 2) {
 
255
                                                char c1 = GetCharAt (pos1 - 2);
 
256
                                                if (c1 == '\n' || c1 == '\r')
 
257
                                                        newLine = c1 + newLine;
 
258
                                        }
 
259
                                        if (newLine == null)
 
260
                                                newLine = Environment.NewLine;
 
261
                                }
 
262
                                return newLine;
 
263
                        }
275
264
                }
276
265
                
277
266
                public int GetLineLength (int line)
314
303
                        textBuffer.GetLineColumnFromPosition (offset, out openingLine, out col);
315
304
                        return offset;
316
305
                }
 
306
                
 
307
                public static char GetMatchingBrace (char ch)
 
308
                {
 
309
                        switch (ch) {
 
310
                        case '(':
 
311
                                return ')';
 
312
                        case ')':
 
313
                                return '(';
 
314
                        case '{':
 
315
                                return '}';
 
316
                        case '}':
 
317
                                return '{';
 
318
                        case '[':
 
319
                                return ']';
 
320
                        case ']':
 
321
                                return '[';
 
322
                        }
 
323
                        throw new System.ArgumentException (ch.ToString (), "ch");
 
324
                }
 
325
                public static bool IsBrace (char ch)
 
326
                {
 
327
                        return ch == '(' || ch == ')' || ch == '{' || ch == '}' || ch == '[' || ch == ']';
 
328
                }
 
329
                public static bool IsOpenBrace (char ch)
 
330
                {
 
331
                        return ch == '(' || ch == '{' || ch == '[';
 
332
                }
 
333
 
 
334
                public int SearchBracketForward (int offset, char openBracket, char closingBracket)
 
335
                {
 
336
                        return MonoDevelop.Projects.Gui.Completion.TextUtilities.SearchBracketForward (completionWidget, offset, openBracket, closingBracket);
 
337
                }
 
338
                
 
339
                public int SearchBracketBackward (int offset, char openBracket, char closingBracket)
 
340
                {
 
341
                        return MonoDevelop.Projects.Gui.Completion.TextUtilities.SearchBracketBackward (completionWidget, offset, openBracket, closingBracket);
 
342
                }
 
343
 
 
344
                public CodeCompletionContext CurrentCodeCompletionContext {
 
345
                        get {
 
346
                                if (completionWidget == null)
 
347
                                        return null;
 
348
                                return completionWidget.CreateCodeCompletionContext (this.CursorPosition);
 
349
                        }
 
350
                }
 
351
                
 
352
                public int SearchChar (int startPos, char searchChar)
 
353
                {
 
354
                        bool isInString = false, isInChar = false;
 
355
                        bool isInLineComment  = false, isInBlockComment = false;
 
356
                        
 
357
                        for (int pos = startPos; pos < TextLength; pos++) {
 
358
                                char ch = GetCharAt (pos);
 
359
                                switch (ch) {
 
360
                                        case '\r':
 
361
                                        case '\n':
 
362
                                                isInLineComment = false;
 
363
                                                break;
 
364
                                        case '/':
 
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);
 
370
                                                        if (nextChar == '/')
 
371
                                                                isInLineComment = true;
 
372
                                                        if (!isInLineComment && nextChar == '*')
 
373
                                                                isInBlockComment = true;
 
374
                                                }
 
375
                                                break;
 
376
                                        case '"':
 
377
                                                if (!(isInChar || isInLineComment || isInBlockComment)) 
 
378
                                                        isInString = !isInString;
 
379
                                                break;
 
380
                                        case '\'':
 
381
                                                if (!(isInString || isInLineComment || isInBlockComment)) 
 
382
                                                        isInChar = !isInChar;
 
383
                                                break;
 
384
                                        default :
 
385
                                                if (ch == searchChar) {
 
386
                                                        if (!(isInString || isInChar || isInLineComment || isInBlockComment))
 
387
                                                                return pos;
 
388
                                                }
 
389
                                                break;
 
390
                                }
 
391
                        }
 
392
                        return -1;
 
393
                }
 
394
                
 
395
                public void GotoMatchingBrace ()
 
396
                {
 
397
                        int offset = textBuffer.CursorPosition;
 
398
                        if (!IsBrace (textBuffer.GetCharAt (offset)))
 
399
                                offset--;
 
400
                        
 
401
                        if (offset >= 0 && IsBrace (textBuffer.GetCharAt (offset))) {
 
402
                                char open = textBuffer.GetCharAt (offset);
 
403
                                char close = open;
 
404
                                bool searchForward = IsOpenBrace (open);
 
405
                                if (searchForward) {
 
406
                                        close = GetMatchingBrace (open);
 
407
                                        offset = SearchBracketForward (offset + 1, open, close);
 
408
                                } else {
 
409
                                        open  = GetMatchingBrace (open);
 
410
                                        offset = SearchBracketBackward (offset - 1, open, close);
 
411
                                }
 
412
                                if (offset >= 0)
 
413
                                        textBuffer.CursorPosition = offset;
 
414
                        }
 
415
                }
317
416
        }
318
417
}