-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
AceEditor.java
1590 lines (1355 loc) · 45.5 KB
/
AceEditor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* AceEditor.java
*
* Copyright (C) 2009-12 by RStudio, Inc.
*
* This program is licensed to you under the terms of version 3 of the
* GNU Affero General Public License. This program is distributed WITHOUT
* ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
* AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
*
*/
package org.rstudio.studio.client.workbench.views.source.editors.text;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.RepeatingCommand;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.PreElement;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import org.rstudio.core.client.CommandWithArg;
import org.rstudio.core.client.ExternalJavaScriptLoader;
import org.rstudio.core.client.ExternalJavaScriptLoader.Callback;
import org.rstudio.core.client.Rectangle;
import org.rstudio.core.client.StringUtil;
import org.rstudio.core.client.command.KeyboardShortcut;
import org.rstudio.core.client.dom.DomUtils;
import org.rstudio.core.client.regex.Match;
import org.rstudio.core.client.regex.Pattern;
import org.rstudio.core.client.widget.DynamicIFrame;
import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.common.codetools.CodeToolsServerOperations;
import org.rstudio.studio.client.common.filetypes.TextFileType;
import org.rstudio.studio.client.server.Void;
import org.rstudio.studio.client.workbench.model.ChangeTracker;
import org.rstudio.studio.client.workbench.model.EventBasedChangeTracker;
import org.rstudio.studio.client.workbench.views.console.shell.assist.CompletionManager;
import org.rstudio.studio.client.workbench.views.console.shell.assist.CompletionManager.InitCompletionFilter;
import org.rstudio.studio.client.workbench.views.console.shell.assist.CompletionPopupPanel;
import org.rstudio.studio.client.workbench.views.console.shell.assist.NullCompletionManager;
import org.rstudio.studio.client.workbench.views.console.shell.assist.RCompletionManager;
import org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorDisplay;
import org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorPosition;
import org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorSelection;
import org.rstudio.studio.client.workbench.views.console.shell.editor.InputEditorUtil;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.*;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceClickEvent.Handler;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.Mode.InsertChunkInfo;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.Renderer.ScreenCoordinates;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.spelling.CharClassifier;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.spelling.TokenPredicate;
import org.rstudio.studio.client.workbench.views.source.editors.text.ace.spelling.WordIterable;
import org.rstudio.studio.client.workbench.views.source.editors.text.events.*;
import org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionEvent;
import org.rstudio.studio.client.workbench.views.source.events.RecordNavigationPositionHandler;
import org.rstudio.studio.client.workbench.views.source.model.RnwCompletionContext;
import org.rstudio.studio.client.workbench.views.source.model.SourcePosition;
public class AceEditor implements DocDisplay,
InputEditorDisplay,
NavigableSourceEditor
{
public enum NewLineMode
{
Windows("windows"),
Unix("unix"),
Auto("auto");
NewLineMode(String type)
{
this.type = type;
}
public String getType()
{
return type;
}
private String type;
}
private class Filter implements InitCompletionFilter
{
public boolean shouldComplete(NativeEvent event)
{
// Never complete if there's an active selection
Range range = getSession().getSelection().getRange();
if (!range.isEmpty())
return false;
// Don't consider Tab to be a completion if we're at the start of a
// line (e.g. only zero or more whitespace characters between the
// beginning of the line and the cursor)
if (event != null && event.getKeyCode() != KeyCodes.KEY_TAB)
return true;
int col = range.getStart().getColumn();
if (col == 0)
return false;
String row = getSession().getLine(range.getStart().getRow());
return row.substring(0, col).trim().length() != 0;
}
}
private class AnchoredSelectionImpl implements AnchoredSelection
{
private AnchoredSelectionImpl(Anchor start, Anchor end)
{
start_ = start;
end_ = end;
}
public String getValue()
{
return getSession().getTextRange(getRange());
}
public void apply()
{
getSession().getSelection().setSelectionRange(
getRange());
}
public Range getRange()
{
return Range.fromPoints(start_.getPosition(), end_.getPosition());
}
public void detach()
{
start_.detach();
end_.detach();
}
private final Anchor start_;
private final Anchor end_;
}
private class AceEditorChangeTracker extends EventBasedChangeTracker<Void>
{
private AceEditorChangeTracker()
{
super(AceEditor.this);
AceEditor.this.addFoldChangeHandler(new org.rstudio.studio.client.workbench.views.source.editors.text.events.FoldChangeEvent.Handler()
{
@Override
public void onFoldChange(FoldChangeEvent event)
{
changed_ = true;
}
});
}
@Override
public ChangeTracker fork()
{
AceEditorChangeTracker forked = new AceEditorChangeTracker();
forked.changed_ = changed_;
return forked;
}
}
public static void preload()
{
load(null);
}
public static void load(final Command command)
{
aceLoader_.addCallback(new Callback()
{
public void onLoaded()
{
aceSupportLoader_.addCallback(new Callback()
{
public void onLoaded()
{
if (command != null)
command.execute();
}
});
}
});
}
@Inject
public AceEditor()
{
widget_ = new AceEditorWidget();
completionManager_ = new NullCompletionManager();
RStudioGinjector.INSTANCE.injectMembers(this);
widget_.addValueChangeHandler(new ValueChangeHandler<Void>()
{
public void onValueChange(ValueChangeEvent<Void> evt)
{
ValueChangeEvent.fire(AceEditor.this, null);
}
});
widget_.addFoldChangeHandler(new FoldChangeEvent.Handler()
{
@Override
public void onFoldChange(FoldChangeEvent event)
{
AceEditor.this.fireEvent(new FoldChangeEvent());
}
});
addCapturingKeyDownHandler(new KeyDownHandler()
{
@Override
public void onKeyDown(KeyDownEvent event)
{
if (useVimMode_)
return;
int mod = KeyboardShortcut.getModifierValue(event.getNativeEvent());
if (mod == KeyboardShortcut.CTRL)
{
switch (event.getNativeKeyCode())
{
case 'U':
event.preventDefault();
InputEditorUtil.yankBeforeCursor(AceEditor.this, true);
break;
case 'K':
event.preventDefault();
InputEditorUtil.yankAfterCursor(AceEditor.this, true);
break;
case 'Y':
event.preventDefault();
Position start = getSelectionStart();
InputEditorUtil.pasteYanked(AceEditor.this);
indentPastedRange(Range.fromPoints(start,
getSelectionEnd()));
break;
}
}
}
});
addPasteHandler(new PasteEvent.Handler()
{
@Override
public void onPaste(PasteEvent event)
{
final Position start = getSelectionStart();
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
@Override
public void execute()
{
Range range = Range.fromPoints(start, getSelectionEnd());
indentPastedRange(range);
}
});
}
});
// handle click events
addAceClickHandler(new AceClickEvent.Handler()
{
@Override
public void onClick(AceClickEvent event)
{
fixVerticalOffsetBug();
if (DomUtils.isCommandClick(event.getNativeEvent()))
{
// eat the event so ace doesn't do anything with it
event.preventDefault();
event.stopPropagation();
// set the cursor position
setCursorPosition(event.getDocumentPosition());
// go to function definition
fireEvent(new CommandClickEvent());
}
}
});
addCursorChangedHandler(new CursorChangedHandler()
{
@Override
public void onCursorChanged(CursorChangedEvent event)
{
fixVerticalOffsetBug();
}
});
}
private void indentPastedRange(Range range)
{
if (fileType_ == null ||
!fileType_.canAutoIndent() ||
!RStudioGinjector.INSTANCE.getUIPrefs().reindentOnPaste().getValue())
{
return;
}
String firstLinePrefix = getSession().getTextRange(
Range.fromPoints(Position.create(range.getStart().getRow(), 0),
range.getStart()));
if (firstLinePrefix.trim().length() != 0)
{
Position newStart = Position.create(range.getStart().getRow() + 1, 0);
if (newStart.compareTo(range.getEnd()) >= 0)
return;
range = Range.fromPoints(newStart, range.getEnd());
}
getSession().reindent(range);
}
@Inject
void initialize(CodeToolsServerOperations server)
{
server_ = server;
}
public TextFileType getFileType()
{
return fileType_;
}
public void setFileType(TextFileType fileType)
{
setFileType(fileType, false);
}
public void setFileType(TextFileType fileType, boolean suppressCompletion)
{
fileType_ = fileType;
updateLanguage(suppressCompletion);
}
public void setFileType(TextFileType fileType,
CompletionManager completionManager)
{
fileType_ = fileType;
updateLanguage(completionManager);
}
@Override
public void setRnwCompletionContext(RnwCompletionContext rnwContext)
{
rnwContext_ = rnwContext;
}
private void updateLanguage(boolean suppressCompletion)
{
if (fileType_ == null)
return;
CompletionManager completionManager;
if (!suppressCompletion && fileType_.getEditorLanguage().useRCompletion())
{
completionManager = new RCompletionManager(
this,
this,
new CompletionPopupPanel(),
server_,
new Filter(),
fileType_.canExecuteChunks() ? rnwContext_ : null);
}
else
completionManager = new NullCompletionManager();
updateLanguage(completionManager);
}
private void updateLanguage(CompletionManager completionManager)
{
if (fileType_ == null)
return;
completionManager_ = completionManager;
updateKeyboardHandlers();
getSession().setEditorMode(
fileType_.getEditorLanguage().getParserName(),
false);
getSession().setUseWrapMode(fileType_.getWordWrap());
}
private void updateKeyboardHandlers()
{
// create a keyboard previewer for our special hooks
AceKeyboardPreviewer previewer = new AceKeyboardPreviewer(
completionManager_);
// reset keyboard handlers
widget_.getEditor().setKeyboardHandler(null);
// if required add vim handlers (to main editor and our previewer)
if (useVimMode_)
{
widget_.getEditor().addKeyboardHandler(KeyboardHandler.vim());
previewer.addHandler(
new AceVimCommandHandler(new CommandWithArg<Boolean>() {
@Override
public void execute(Boolean arg)
{
fireEvent(new FindRequestedEvent(arg));
}
}));
}
// add the previewer's handler
widget_.getEditor().addKeyboardHandler(previewer.getKeyboardHandler());
}
public String getCode()
{
return getSession().getValue();
}
public void setCode(String code, boolean preserveCursorPosition)
{
// Calling setCode("", false) while the editor contains multiple lines of
// content causes bug 2928: Flickering console when typing. Empirically,
// first setting code to a single line of content and then clearing it,
// seems to correct this problem.
if (StringUtil.isNullOrEmpty(code))
doSetCode(" ", preserveCursorPosition);
doSetCode(code, preserveCursorPosition);
}
private void doSetCode(String code, boolean preserveCursorPosition)
{
// Filter out Escape characters that might have snuck in from an old
// bug in 0.95. We can choose to remove this when 0.95 ships, hopefully
// any documents that would be affected by this will be gone by then.
code = code.replaceAll("\u001B", "");
final AceEditorNative ed = widget_.getEditor();
if (preserveCursorPosition)
{
final Position cursorPos;
final int scrollTop, scrollLeft;
cursorPos = ed.getSession().getSelection().getCursor();
scrollTop = ed.getRenderer().getScrollTop();
scrollLeft = ed.getRenderer().getScrollLeft();
// Setting the value directly on the document prevents undo/redo
// stack from being blown away
widget_.getEditor().getSession().getDocument().setValue(code);
ed.getSession().getSelection().moveCursorTo(cursorPos.getRow(),
cursorPos.getColumn(),
false);
ed.getRenderer().scrollToY(scrollTop);
ed.getRenderer().scrollToX(scrollLeft);
Scheduler.get().scheduleDeferred(new ScheduledCommand()
{
@Override
public void execute()
{
ed.getRenderer().scrollToY(scrollTop);
ed.getRenderer().scrollToX(scrollLeft);
}
});
}
else
{
ed.getSession().setValue(code);
ed.getSession().getSelection().moveCursorTo(0, 0, false);
}
}
public int getScrollLeft()
{
return widget_.getEditor().getRenderer().getScrollLeft();
}
public void scrollToX(int x)
{
widget_.getEditor().getRenderer().scrollToX(x);
}
public int getScrollTop()
{
return widget_.getEditor().getRenderer().getScrollTop();
}
public void scrollToY(int y)
{
widget_.getEditor().getRenderer().scrollToY(y);
}
public void insertCode(String code, boolean blockMode)
{
// TODO: implement block mode
getSession().replace(
getSession().getSelection().getRange(), code);
}
public String getCode(Position start, Position end)
{
return getSession().getTextRange(Range.fromPoints(start, end));
}
@Override
public InputEditorSelection search(String needle,
boolean backwards,
boolean wrap,
boolean caseSensitive,
boolean wholeWord,
Position start,
Range range,
boolean regexpMode)
{
Search search = Search.create(needle,
backwards,
wrap,
caseSensitive,
wholeWord,
start,
range,
regexpMode);
Range resultRange = search.find(getSession());
if (resultRange != null)
{
return createSelection(resultRange.getStart(), resultRange.getEnd());
}
else
{
return null;
}
}
@Override
public void insertCode(InputEditorPosition position, String content)
{
getSession().insert(selectionToPosition(position), content);
}
@Override
public String getCode(InputEditorSelection selection)
{
return getCode(((AceInputEditorPosition)selection.getStart()).getValue(),
((AceInputEditorPosition)selection.getEnd()).getValue());
}
public void focus()
{
widget_.getEditor().focus();
}
public void codeCompletion()
{
completionManager_.codeCompletion();
}
public void goToHelp()
{
completionManager_.goToHelp();
}
public void goToFunctionDefinition()
{
completionManager_.goToFunctionDefinition();
}
class PrintIFrame extends DynamicIFrame
{
public PrintIFrame(String code, double fontSize)
{
code_ = code;
fontSize_ = fontSize;
getElement().getStyle().setPosition(com.google.gwt.dom.client.Style.Position.ABSOLUTE);
getElement().getStyle().setLeft(-5000, Unit.PX);
}
@Override
protected void onFrameLoaded()
{
Document doc = getDocument();
PreElement pre = doc.createPreElement();
pre.setInnerText(code_);
pre.getStyle().setProperty("whiteSpace", "pre-wrap");
pre.getStyle().setFontSize(fontSize_, Unit.PT);
doc.getBody().appendChild(pre);
getWindow().print();
// Bug 1224: ace: print from source causes inability to reconnect
// This was caused by the iframe being removed from the document too
// quickly after the print job was sent. As a result, attempting to
// navigate away from the page at any point afterwards would result
// in the error "Document cannot change while printing or in Print
// Preview". The only thing you could do is close the browser tab.
// By inserting a 5-minute delay hopefully Firefox would be done with
// whatever print related operations are important.
Scheduler.get().scheduleFixedDelay(new RepeatingCommand()
{
public boolean execute()
{
PrintIFrame.this.removeFromParent();
return false;
}
}, 1000 * 60 * 5);
}
private final String code_;
private final double fontSize_;
}
public void print()
{
PrintIFrame printIFrame = new PrintIFrame(
getCode(),
RStudioGinjector.INSTANCE.getUIPrefs().fontSize().getValue());
RootPanel.get().add(printIFrame);
}
public String getText()
{
return getSession().getLine(
getSession().getSelection().getCursor().getRow());
}
public void setText(String string)
{
setCode(string, false);
getSession().getSelection().moveCursorFileEnd();
}
public boolean hasSelection()
{
return true;
}
public InputEditorSelection getSelection()
{
Range selection = getSession().getSelection().getRange();
return new InputEditorSelection(
new AceInputEditorPosition(getSession(), selection.getStart()),
new AceInputEditorPosition(getSession(), selection.getEnd()));
}
public String getSelectionValue()
{
return getSession().getTextRange(
getSession().getSelection().getRange());
}
public Position getSelectionStart()
{
return getSession().getSelection().getRange().getStart();
}
public Position getSelectionEnd()
{
return getSession().getSelection().getRange().getEnd();
}
@Override
public Range getSelectionRange()
{
return Range.fromPoints(getSelectionStart(), getSelectionEnd());
}
@Override
public void setSelectionRange(Range range)
{
getSession().getSelection().setSelectionRange(range);
}
public int getLength(int row)
{
return getSession().getDocument().getLine(row).length();
}
public int getRowCount()
{
return getSession().getDocument().getLength();
}
public String getLine(int row)
{
return getSession().getLine(row);
}
@Override
public InputEditorSelection createSelection(Position pos1, Position pos2)
{
return new InputEditorSelection(
new AceInputEditorPosition(getSession(), pos1),
new AceInputEditorPosition(getSession(), pos2));
}
@Override
public Position selectionToPosition(InputEditorPosition pos)
{
// HACK: This cast is gross, InputEditorPosition should just become
// AceInputEditorPosition
return Position.create((Integer) pos.getLine(), pos.getPosition());
}
@Override
public Iterable<Range> getWords(TokenPredicate tokenPredicate,
CharClassifier charClassifier,
Position start,
Position end)
{
return new WordIterable(getSession(),
tokenPredicate,
charClassifier,
start,
end);
}
@Override
public String getTextForRange(Range range)
{
return getSession().getTextRange(range);
}
@Override
public Anchor createAnchor(Position pos)
{
return Anchor.createAnchor(getSession().getDocument(),
pos.getRow(),
pos.getColumn());
}
private void fixVerticalOffsetBug()
{
widget_.getEditor().getRenderer().fixVerticalOffsetBug();
}
@Override
public String debug_getDocumentDump()
{
return widget_.getEditor().getSession().getDocument().getDocumentDump();
}
@Override
public void debug_setSessionValueDirectly(String s)
{
widget_.getEditor().getSession().setValue(s);
}
public void setSelection(InputEditorSelection selection)
{
AceInputEditorPosition start = (AceInputEditorPosition)selection.getStart();
AceInputEditorPosition end = (AceInputEditorPosition)selection.getEnd();
getSession().getSelection().setSelectionRange(Range.fromPoints(
start.getValue(), end.getValue()));
}
public Rectangle getCursorBounds()
{
Range range = getSession().getSelection().getRange();
Renderer renderer = widget_.getEditor().getRenderer();
ScreenCoordinates start = renderer.textToScreenCoordinates(
range.getStart().getRow(),
range.getStart().getColumn());
ScreenCoordinates end = renderer.textToScreenCoordinates(
range.getEnd().getRow(),
range.getEnd().getColumn());
return new Rectangle(start.getPageX(),
start.getPageY(),
end.getPageX() - start.getPageX(),
renderer.getLineHeight());
}
public Rectangle getPositionBounds(InputEditorPosition position)
{
Renderer renderer = widget_.getEditor().getRenderer();
Position pos = ((AceInputEditorPosition) position).getValue();
ScreenCoordinates start = renderer.textToScreenCoordinates(
pos.getRow(),
pos.getColumn());
return new Rectangle(start.getPageX(), start.getPageY(),
(int) Math.round(renderer.getCharacterWidth()),
(int) (renderer.getLineHeight() * 0.8));
}
public Rectangle getBounds()
{
return new Rectangle(
widget_.getAbsoluteLeft(),
widget_.getAbsoluteTop(),
widget_.getOffsetWidth(),
widget_.getOffsetHeight());
}
public void setFocus(boolean focused)
{
if (focused)
widget_.getEditor().focus();
else
widget_.getEditor().blur();
}
public String replaceSelection(String value, boolean collapseSelection)
{
Selection selection = getSession().getSelection();
String oldValue = getSession().getTextRange(selection.getRange());
replaceSelection(value);
if (collapseSelection)
{
collapseSelection(false);
}
return oldValue;
}
public boolean isSelectionCollapsed()
{
return getSession().getSelection().isEmpty();
}
public boolean isCursorAtEnd()
{
int lastRow = getRowCount() - 1;
Position cursorPos = getCursorPosition();
return cursorPos.compareTo(Position.create(lastRow,
getLength(lastRow))) == 0;
}
public void clear()
{
setCode("", false);
}
public void collapseSelection(boolean collapseToStart)
{
Selection selection = getSession().getSelection();
Range rng = selection.getRange();
Position pos = collapseToStart ? rng.getStart() : rng.getEnd();
selection.setSelectionRange(Range.fromPoints(pos, pos));
}
public InputEditorSelection getStart()
{
return new InputEditorSelection(
new AceInputEditorPosition(getSession(), Position.create(0, 0)));
}
public InputEditorSelection getEnd()
{
EditSession session = getSession();
int rows = session.getLength();
Position end = Position.create(rows, session.getLine(rows).length());
return new InputEditorSelection(new AceInputEditorPosition(session, end));
}
public String getCurrentLine()
{
int row = getSession().getSelection().getRange().getStart().getRow();
return getSession().getLine(row);
}
public int getCurrentLineNum()
{
Position pos = getCursorPosition();
return getSession().documentToScreenRow(pos);
}
public int getCurrentLineCount()
{
return getSession().getScreenLength();
}
@Override
public String getLanguageMode(Position position)
{
return getSession().getMode().getLanguageMode(position);
}
public void replaceCode(String code)
{
int endRow, endCol;
endRow = getSession().getLength() - 1;
if (endRow < 0)
{
endRow = 0;
endCol = 0;
}
else
{
endCol = getSession().getLine(endRow).length();
}
Range range = Range.fromPoints(Position.create(0, 0),
Position.create(endRow, endCol));
getSession().replace(range, code);
}
public void replaceSelection(String code)
{
Range selRange = getSession().getSelection().getRange();
Position position = getSession().replace(selRange, code);
Range range = Range.fromPoints(selRange.getStart(), position);
getSession().getSelection().setSelectionRange(range);
}
public boolean moveSelectionToNextLine(boolean skipBlankLines)
{
int curRow = getSession().getSelection().getCursor().getRow();
while (++curRow < getSession().getLength())
{
String line = getSession().getLine(curRow);
Pattern pattern = Pattern.create("[^\\s]");
Match match = pattern.match(line, 0);
if (skipBlankLines && match == null)
continue;
int col = (match != null) ? match.getIndex() : 0;
getSession().getSelection().moveCursorTo(curRow, col, false);
getSession().unfold(curRow, true);
return true;
}
return false;
}
@Override
public boolean moveSelectionToBlankLine()
{
int curRow = getSession().getSelection().getCursor().getRow();
while (curRow < getSession().getLength())
{
String line = getSession().getLine(curRow);
if (line.length() == 0)
{
getSession().getSelection().moveCursorTo(curRow, 0, false);
getSession().unfold(curRow, true);
return true;
}
curRow++;
}
return false;
}
@Override
public void reindent()
{
boolean emptySelection = getSelection().isEmpty();
getSession().reindent(getSession().getSelection().getRange());
if (emptySelection)
moveSelectionToNextLine(false);
}
@Override
public void toggleCommentLines()
{
widget_.getEditor().toggleCommentLines();
}
public ChangeTracker getChangeTracker()
{
return new AceEditorChangeTracker();
}
public AnchoredSelection createAnchoredSelection(Position startPos,
Position endPos)
{
Anchor start = Anchor.createAnchor(getSession().getDocument(),
startPos.getRow(),