Advertisement
Guest User

Untitled

a guest
May 27th, 2018
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 26.17 KB | None | 0 0
  1. // Copyright (c) 2011-2014, David H. Hovemeyer <david.hovemeyer@gmail.com>
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20.  
  21. package edu.ycp.cs.dh.acegwt.client.ace;
  22.  
  23. import com.google.gwt.core.client.JavaScriptObject;
  24. import com.google.gwt.core.client.JsArray;
  25. import com.google.gwt.dom.client.Element;
  26. import com.google.gwt.user.client.TakesValue;
  27. import com.google.gwt.user.client.ui.Composite;
  28. import com.google.gwt.user.client.ui.FlowPanel;
  29. import com.google.gwt.user.client.ui.HasText;
  30. import com.google.gwt.user.client.ui.RequiresResize;
  31.  
  32. import java.util.Collections;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36.  
  37. /**
  38. * A GWT widget for the Ajax.org Code Editor (ACE).
  39. *
  40. * @see <a href="http://ace.ajax.org/">Ajax.org Code Editor</a>
  41. */
  42. public class AceEditor extends Composite implements RequiresResize, HasText, TakesValue<String> {
  43. // Used to generate unique element ids for Ace widgets.
  44. private static int nextId = 0;
  45.  
  46. private final String elementId;
  47.  
  48. private JavaScriptObject editor;
  49.  
  50. private JsArray<AceAnnotation> annotations = JavaScriptObject.createArray().cast();
  51.  
  52. private Element divElement;
  53.  
  54. private HashMap<Integer, AceRange> markers = new HashMap<Integer, AceRange>();
  55.  
  56. private AceSelection selection = null;
  57.  
  58. private AceCommandLine commandLine = null;
  59.  
  60. /**
  61. * Preferred constructor.
  62. */
  63. public AceEditor() {
  64. elementId = "_aceGWT" + nextId;
  65. nextId++;
  66. FlowPanel div = new FlowPanel();
  67. div.getElement().setId(elementId);
  68. initWidget(div);
  69. divElement = div.getElement();
  70. }
  71.  
  72. /**
  73. * Do not use this constructor: just use the default constructor.
  74. * @param unused this parameter is ignored
  75. */
  76. @Deprecated
  77. public AceEditor(boolean unused) {
  78. this();
  79. }
  80.  
  81. /**
  82. * Call this method to start the editor.
  83. * Make sure that the widget has been attached to the DOM tree
  84. * before calling this method.
  85. */
  86. public native void startEditor() /*-{
  87. var editor = $wnd.ace.edit(this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::divElement);
  88. editor.getSession().setUseWorker(false);
  89. this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor = editor;
  90.  
  91. // Store a reference to the (Java) AceEditor object in the
  92. // JavaScript editor object.
  93. editor._aceGWTAceEditor = this;
  94.  
  95. // I have been noticing sporadic failures of the editor
  96. // to display properly and receive key/mouse events.
  97. // Try to force the editor to resize and display itself fully. See:
  98. // https://groups.google.com/group/ace-discuss/browse_thread/thread/237262b521dcea33
  99. editor.resize();
  100. this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::redisplay();
  101. }-*/;
  102.  
  103. /**
  104. * Call this to force the editor contents to be redisplayed.
  105. * There seems to be a problem when an AceEditor is embedded in a LayoutPanel:
  106. * the editor contents don't appear, and it refuses to accept focus
  107. * and mouse events, until the browser window is resized.
  108. * Calling this method works around the problem by forcing
  109. * the underlying editor to redisplay itself fully. (?)
  110. */
  111. public native void redisplay() /*-{
  112. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  113. editor.renderer.onResize(true);
  114. editor.renderer.updateFull();
  115. editor.resize();
  116. editor.focus();
  117. }-*/;
  118.  
  119. /**
  120. * Cleans up the entire editor.
  121. */
  122. public native void destroy() /*-{
  123. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  124. editor.destroy();
  125. }-*/;
  126.  
  127. /**
  128. * Set the theme.
  129. *
  130. * @param theme the theme (one of the values in the {@link AceEditorTheme}
  131. * enumeration)
  132. */
  133. public void setTheme(final AceEditorTheme theme) {
  134. setThemeByName(theme.getName());
  135. }
  136.  
  137. /**
  138. * Set the theme by name.
  139. *
  140. * @param themeName the theme name (e.g., "twilight")
  141. */
  142. public native void setThemeByName(String themeName) /*-{
  143. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  144. editor.setTheme("ace/theme/" + themeName);
  145. }-*/;
  146.  
  147. /**
  148. * Set the mode.
  149. *
  150. * @param mode the mode (one of the values in the
  151. * {@link AceEditorMode} enumeration)
  152. */
  153. public void setMode(final AceEditorMode mode) {
  154. setModeByName(mode.getName());
  155. }
  156.  
  157. /**
  158. * Set the mode by name.
  159. *
  160. * @param shortModeName name of mode (e.g., "eclipse")
  161. */
  162. public native void setModeByName(String shortModeName) /*-{
  163. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  164. var modeName = "ace/mode/" + shortModeName;
  165. var TheMode = $wnd.ace.require(modeName).Mode;
  166. editor.getSession().setMode(new TheMode());
  167. }-*/;
  168.  
  169. /**
  170. * Enable a worker for the current session.
  171. *
  172. * @param userWorker true to enable a worker otherwise false
  173. */
  174. public native void setUseWorker(boolean useWorker) /*-{
  175. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  176. editor.getSession().setUseWorker(useWorker);
  177. }-*/;
  178.  
  179. /**
  180. * Register a handler for change events generated by the editor.
  181. *
  182. * @param callback the change event handler
  183. */
  184. public native void addOnChangeHandler(AceEditorCallback callback) /*-{
  185. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  186. editor.getSession().on("change", function(e) {
  187. callback.@edu.ycp.cs.dh.acegwt.client.ace.AceEditorCallback::invokeAceCallback(Lcom/google/gwt/core/client/JavaScriptObject;)(e);
  188. });
  189. }-*/;
  190.  
  191. /**
  192. * Register a handler for cursor position change events generated by the editor.
  193. *
  194. * @param callback the cursor position change event handler
  195. */
  196. public native void addOnCursorPositionChangeHandler(AceEditorCallback callback) /*-{
  197. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  198. editor.getSession().selection.on("changeCursor", function(e) {
  199. callback.@edu.ycp.cs.dh.acegwt.client.ace.AceEditorCallback::invokeAceCallback(Lcom/google/gwt/core/client/JavaScriptObject;)(e);
  200. });
  201. }-*/;
  202.  
  203. /**
  204. * Give font size
  205. * @return font size
  206. */
  207. public native int getFontSize() /*-{
  208. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  209. return editor.getFontSize();
  210. }-*/;
  211.  
  212. /**
  213. * Set font size.
  214. * @param fontSize the font size to set, e.g., "16px"
  215. */
  216. public native void setFontSize(String fontSize) /*-{
  217. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  218. editor.setFontSize(fontSize);
  219. }-*/;
  220.  
  221. /**
  222. * Set integer font size.
  223. * @param fontSize the font size to set, e.g., 16
  224. */
  225. public native void setFontSize(int fontSize) /*-{
  226. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  227. editor.setFontSize(fontSize);
  228. }-*/;
  229.  
  230. /**
  231. * Get the complete text in the editor as a String.
  232. *
  233. * @return the text in the editor
  234. */
  235. public native String getText() /*-{
  236. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  237. return editor.getSession().getValue();
  238. }-*/;
  239.  
  240. /**
  241. * Causes the editor to gain input focus.
  242. */
  243. public native void focus() /*-{
  244. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  245. editor.focus();
  246. }-*/;
  247.  
  248. /**
  249. * Retrieves the number of lines in the editor.
  250. *
  251. * @return The number of lines in the editor.
  252. */
  253. public native int getLineCount() /*-{
  254. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  255. return editor.session.getLength();
  256. }-*/;
  257.  
  258. /**
  259. * Set the complete text in the editor from a String.
  260. *
  261. * @param text the text to set in the editor
  262. */
  263. public native void setText(String text) /*-{
  264. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  265. editor.getSession().setValue(text);
  266. }-*/;
  267.  
  268. /**
  269. * Get the line of text at the given row number.
  270. *
  271. * @param row the row number
  272. * @return the line of text at that row number
  273. */
  274. public native String getLine(int row) /*-{
  275. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  276. return editor.getSession().getDocument().getLine(row);
  277. }-*/;
  278.  
  279. /**
  280. * Insert given text at the cursor.
  281. *
  282. * @param text text to insert at the cursor
  283. */
  284. public native void insertAtCursor(String text) /*-{
  285. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  286. editor.insert(text);
  287. }-*/;
  288.  
  289. /**
  290. * Get the current cursor position.
  291. *
  292. * @return the current cursor position
  293. */
  294. public native AceEditorCursorPosition getCursorPosition() /*-{
  295. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  296. var pos = editor.getCursorPosition();
  297. return this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::getCursorPositionImpl(DD)(pos.row, pos.column);
  298. }-*/;
  299.  
  300. private AceEditorCursorPosition getCursorPositionImpl(final double row, final double column) {
  301. return new AceEditorCursorPosition((int) row, (int) column);
  302. }
  303.  
  304. /**
  305. * Gets the given document position as a zero-based index.
  306. *
  307. * @param position the position to obtain the absolute index of (base zero)
  308. * @return An index to the current location in the document
  309. */
  310. public int getIndexFromPosition(AceEditorCursorPosition position) {
  311. return getIndexFromPositionImpl(position.toJsObject());
  312. }
  313.  
  314. private native int getIndexFromPositionImpl(JavaScriptObject jsPosition) /*-{
  315. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  316. return editor.getSession().getDocument().positionToIndex(jsPosition);
  317. }-*/;
  318.  
  319. /**
  320. * Gets a document position from a supplied zero-based index.
  321. *
  322. * @param index (base zero)
  323. * @return A position object showing the row and column of the supplied index in the document
  324. */
  325. public native AceEditorCursorPosition getPositionFromIndex(int index) /*-{
  326. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  327. var jsPosition = editor.getSession().getDocument().indexToPosition(index);
  328. return @edu.ycp.cs.dh.acegwt.client.ace.AceEditorCursorPosition::create(II)(
  329. jsPosition.row,
  330. jsPosition.column
  331. );
  332. }-*/;
  333.  
  334. /**
  335. * Set whether or not soft tabs should be used.
  336. *
  337. * @param useSoftTabs true if soft tabs should be used, false otherwise
  338. */
  339. public native void setUseSoftTabs(boolean useSoftTabs) /*-{
  340. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  341. editor.getSession().setUseSoftTabs(useSoftTabs);
  342. }-*/;
  343.  
  344. /**
  345. * Set tab size. (Default is 4.)
  346. *
  347. * @param tabSize the tab size to set
  348. */
  349. public native void setTabSize(int tabSize) /*-{
  350. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  351. editor.getSession().setTabSize(tabSize);
  352. }-*/;
  353.  
  354. /**
  355. * Go to given line.
  356. *
  357. * @param line the line to go to
  358. */
  359. public native void gotoLine(int line) /*-{
  360. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  361. editor.gotoLine(line);
  362. }-*/;
  363.  
  364. /**
  365. * Set whether or not the horizontal scrollbar is always visible.
  366. *
  367. * @param hScrollBarAlwaysVisible true if the horizontal scrollbar is always
  368. * visible, false if it is hidden when not needed
  369. */
  370. public native void setHScrollBarAlwaysVisible(boolean hScrollBarAlwaysVisible) /*-{
  371. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  372. editor.renderer.setHScrollBarAlwaysVisible(hScrollBarAlwaysVisible);
  373. }-*/;
  374.  
  375. /**
  376. * Set whether or not the gutter is shown.
  377. *
  378. * @param showGutter true if the gutter should be shown, false if it should be hidden
  379. */
  380. public native void setShowGutter(boolean showGutter) /*-{
  381. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  382. editor.renderer.setShowGutter(showGutter);
  383. }-*/;
  384.  
  385. /**
  386. * Set or unset read-only mode.
  387. *
  388. * @param readOnly true if editor should be set to readonly, false if the
  389. * editor should be set to read-write
  390. */
  391. public native void setReadOnly(boolean readOnly) /*-{
  392. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  393. editor.setReadOnly(readOnly);
  394. }-*/;
  395.  
  396. /**
  397. * Set or unset highlighting of currently selected word.
  398. *
  399. * @param highlightSelectedWord true to highlight currently selected word, false otherwise
  400. */
  401. public native void setHighlightSelectedWord(boolean highlightSelectedWord) /*-{
  402. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  403. editor.setHighlightSelectedWord(highlightSelectedWord);
  404. }-*/;
  405.  
  406. /**
  407. * Set or unset the visibility of the print margin.
  408. *
  409. * @param showPrintMargin true if the print margin should be shown, false otherwise
  410. */
  411. public native void setShowPrintMargin(boolean showPrintMargin) /*-{
  412. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  413. editor.renderer.setShowPrintMargin(showPrintMargin);
  414. }-*/;
  415.  
  416. /**
  417. * Add an annotation to a the local <code>annotations</code> JsArray&lt;AceAnnotation&gt;, but does not set it on the editor
  418. *
  419. * @param row to which the annotation should be added
  420. * @param column to which the annotation applies
  421. * @param text to display as a tooltip with the annotation
  422. * @param type to be displayed (one of the values in the
  423. * {@link AceAnnotationType} enumeration)
  424. */
  425. public void addAnnotation(final int row, final int column, final String text, final AceAnnotationType type) {
  426. annotations.push(AceAnnotation.create(row, column, text, type.getName()));
  427. }
  428.  
  429. /**
  430. * Set any annotations which have been added via <code>addAnnotation</code> on the editor
  431. */
  432. public native void setAnnotations() /*-{
  433. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  434. var annotations = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::annotations;
  435. editor.getSession().setAnnotations(annotations);
  436. }-*/;
  437.  
  438.  
  439. /**
  440. * Clear any annotations from the editor and reset the local <code>annotations</code> JsArray&lt;AceAnnotation&gt;
  441. */
  442. public native void clearAnnotations() /*-{
  443. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  444. editor.getSession().clearAnnotations();
  445. this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::resetAnnotations()();
  446. }-*/;
  447.  
  448. /**
  449. * Reset any annotations in the local <code>annotations</code> JsArray<AceAnnotation>
  450. */
  451. private void resetAnnotations() {
  452. annotations = JavaScriptObject.createArray().cast();
  453. }
  454.  
  455. /**
  456. * Remove a command from the editor.
  457. *
  458. * @param command the command (one of the values in the
  459. * {@link AceCommand} enumeration)
  460. */
  461. public void removeCommand(final AceCommand command) {
  462. removeCommandByName(command.getName());
  463. }
  464.  
  465. /**
  466. * Execute a command with no arguments. See {@link AceCommand}
  467. * values for example.
  468. * @param command the command (one of the values in the
  469. * {@link AceCommand} enumeration)
  470. */
  471. public void execCommand(AceCommand command) {
  472. execCommand(command, null);
  473. }
  474.  
  475. /**
  476. * Execute a command with arguments (in case args is not null).
  477. * See {@link AceCommand} values for example.
  478. * @param command the command (one of the values in the
  479. * {@link AceCommand} enumeration)
  480. * @param args command arguments (string or map)
  481. */
  482. public void execCommand(AceCommand command, AceCommandArgs args) {
  483. execCommand(command.getName(), args);
  484. }
  485.  
  486. /**
  487. * Execute a command possibly containing string argument.
  488. * @param command the command which could be one or two words separated
  489. * by whitespaces
  490. */
  491. public native void execCommand(String command) /*-{
  492. var parts = command.split(/\s+/);
  493. this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::execCommand(Ljava/lang/String;Ljava/lang/String;)(parts[0], parts[1]);
  494. }-*/;
  495.  
  496. /**
  497. * Execute a command with arguments (in case args is not null).
  498. * @param command one word command
  499. * @param args command argument
  500. */
  501. public void execCommand(String command, String arg) {
  502. execCommandHidden(command, arg);
  503. }
  504.  
  505. /**
  506. * Execute a command with arguments (in case args is not null).
  507. * @param command one word command
  508. * @param args command arguments of type {@link AceCommandArgs}
  509. */
  510. public void execCommand(String command, AceCommandArgs args) {
  511. execCommandHidden(command, args);
  512. }
  513.  
  514. private native void execCommandHidden(String command, Object args) /*-{
  515. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  516. if (args && typeof args !== "string")
  517. args = args.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandArgs::getValue()();
  518. editor.commands.exec(command, editor, args);
  519. editor.focus();
  520. }-*/;
  521.  
  522. /**
  523. * Remove commands, that may not be required, from the editor
  524. *
  525. * @param command to be removed, one of
  526. * "gotoline", "findnext", "findprevious", "find", "replace", "replaceall"
  527. */
  528. public native void removeCommandByName(String command) /*-{
  529. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  530. editor.commands.removeCommand(command);
  531. }-*/;
  532.  
  533. /**
  534. * Construct java wrapper for registered Ace command.
  535. * @param command name of command
  536. * @return command description
  537. */
  538. public native AceCommandDescription getCommandDescription(String command) /*-{
  539. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  540. var obj = editor.commands.commands[command];
  541. if (!obj)
  542. return null;
  543. return @edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::fromJavaScript(Lcom/google/gwt/core/client/JavaScriptObject;)(obj);
  544. }-*/;
  545.  
  546. /**
  547. * List names of all Ace commands.
  548. * @return names of all Ace commands
  549. */
  550. public native List<String> listCommands() /*-{
  551. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  552. var ret = @java.util.ArrayList::new()();
  553. for (var command in editor.commands.commands)
  554. ret.@java.util.ArrayList::add(Ljava/lang/Object;)(command);
  555. return ret;
  556. }-*/;
  557.  
  558. /**
  559. * Add user defined command.
  560. * @param description command description
  561. */
  562. public native void addCommand(AceCommandDescription description) /*-{
  563. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  564. var command = description.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::toJavaScript(Ledu/ycp/cs/dh/acegwt/client/ace/AceEditor;)(this);
  565. editor.commands.addCommand(command);
  566. }-*/;
  567.  
  568. /**
  569. * Set whether to use wrap mode or not
  570. *
  571. * @param useWrapMode true if word wrap should be used, false otherwise
  572. */
  573. public native void setUseWrapMode(boolean useWrapMode) /*-{
  574. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  575. editor.getSession().setUseWrapMode(useWrapMode);
  576. }-*/;
  577.  
  578. /* (non-Javadoc)
  579. * @see com.google.gwt.user.client.ui.ResizeComposite#onResize()
  580. */
  581. @Override
  582. public void onResize() {
  583. redisplay();
  584. }
  585.  
  586. @Override
  587. public void setValue(String value) {
  588. this.setText(value);
  589. }
  590.  
  591. @Override
  592. public String getValue() {
  593. return this.getText();
  594. }
  595.  
  596. /**
  597. * Set whether or not autocomplete is enabled.
  598. *
  599. * @param b true if autocomplete should be enabled, false if not
  600. */
  601. public native void setAutocompleteEnabled(boolean b) /*-{
  602. // See: https://github.com/ajaxorg/ace/wiki/How-to-enable-Autocomplete-in-the-Ace-editor
  603. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  604. if (b) {
  605. $wnd.ace.require("ace/ext/language_tools");
  606. editor.setOptions({ enableBasicAutocompletion: true });
  607. } else {
  608. editor.setOptions({ enableBasicAutocompletion: false });
  609. }
  610. }-*/;
  611.  
  612.  
  613. /**
  614. * Removes all existing completers from the langtools<br><br>
  615. * This can be used to disable all completers including local completers, which can be very useful
  616. * when completers are used on very large files (as the local completer tokenizes every word to put in the selected list).<br><br>
  617. * <strong>NOTE:</strong> This method may be removed, and replaced with another solution. It works at point of check-in, but treat this as unstable for now.
  618. */
  619. public native static void removeAllExistingCompleters() /*-{
  620. var langTools = $wnd.ace.require("ace/ext/language_tools");
  621. langTools.setCompleters([]);
  622. }-*/;
  623.  
  624.  
  625.  
  626. /**
  627. * Add an {@link AceCompletionProvider} to provide
  628. * custom code completions.
  629. *
  630. * <strong>Warning</strong>: this is an experimental feature of AceGWT.
  631. * It is possible that the API will change in an incompatible way
  632. * in future releases.
  633. *
  634. * @param provider the {@link AceCompletionProvider}
  635. */
  636. public native static void addCompletionProvider(AceCompletionProvider provider) /*-{
  637. var langTools = $wnd.ace.require("ace/ext/language_tools");
  638. var completer = {
  639. getCompletions: function(editor, session, pos, prefix, callback) {
  640. var callbackWrapper =
  641. @edu.ycp.cs.dh.acegwt.client.ace.AceEditor::wrapCompletionCallback(Lcom/google/gwt/core/client/JavaScriptObject;)(callback);
  642. var aceEditor = editor._aceGWTAceEditor;
  643. provider.@edu.ycp.cs.dh.acegwt.client.ace.AceCompletionProvider::getProposals(Ledu/ycp/cs/dh/acegwt/client/ace/AceEditor;Ledu/ycp/cs/dh/acegwt/client/ace/AceEditorCursorPosition;Ljava/lang/String;Ledu/ycp/cs/dh/acegwt/client/ace/AceCompletionCallback;)(
  644. aceEditor,
  645. @edu.ycp.cs.dh.acegwt.client.ace.AceEditorCursorPosition::create(II)( pos.row, pos.column ),
  646. prefix,
  647. callbackWrapper
  648. );
  649. },
  650. getDocTooltip: function(item) {
  651. if ( (!item.docHTML) && item.aceGwtHtmlTooltip != null) {
  652. item.docHTML = item.aceGwtHtmlTooltip;
  653. }
  654. }
  655. };
  656. langTools.addCompleter(completer);
  657. }-*/;
  658.  
  659. /**
  660. * Adds a static marker into this editor.
  661. * @param range an {@link AceRange}.
  662. * @param clazz a CSS class that must be applied to the marker.
  663. * @param type an {@link AceMarkerType}.
  664. * @param inFront set to 'true' if the marker must be in front of the text, 'false' otherwise.
  665. * @return The marker ID. This id can be then use to remove a marker from the editor.
  666. */
  667. public native int addMarker(AceRange range, String clazz, AceMarkerType type, boolean inFront) /*-{
  668. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  669. var markerID = editor.getSession().addMarker(range, clazz, type.@edu.ycp.cs.dh.acegwt.client.ace.AceMarkerType::getName()(), inFront);
  670. this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::addMarker(ILedu/ycp/cs/dh/acegwt/client/ace/AceRange;)(markerID, range);
  671. return markerID;
  672. }-*/;
  673.  
  674. /**
  675. * Adds a floating marker into this editor (the marker follows lines changes as insertions, suppressions...).
  676. * @param range an {@link AceRange}.
  677. * @param clazz a CSS class that must be applied to the marker.
  678. * @param type an {@link AceMarkerType}.
  679. * @return The marker ID. This id can be then use to remove a marker from the editor.
  680. */
  681. public native int addFloatingMarker(AceRange range, String clazz, AceMarkerType type) /*-{
  682. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  683. range.start = editor.getSession().doc.createAnchor(range.start);
  684. range.end = editor.getSession().doc.createAnchor(range.end);
  685. return this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::addMarker(Ledu/ycp/cs/dh/acegwt/client/ace/AceRange;Ljava/lang/String;Ledu/ycp/cs/dh/acegwt/client/ace/AceMarkerType;Z)
  686. (
  687. range,
  688. clazz,
  689. type,
  690. false
  691. );
  692. }-*/;
  693.  
  694. /**
  695. * Removes the marker with the specified ID.
  696. * @param markerId the marker ID.
  697. */
  698. public native void removeMarker(int markerId) /*-{
  699. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  700. editor.getSession().removeMarker(markerId);
  701. this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::removeRegisteredMarker(I)(markerId);
  702. }-*/;
  703.  
  704. /**
  705. * Gets all the displayed markers.
  706. * @return An unmodifiable Mapping between markerID and the displayed range.
  707. */
  708. public Map<Integer, AceRange> getMarkers() {
  709. return Collections.unmodifiableMap(this.markers);
  710. }
  711.  
  712. /**
  713. * Remove all the displayed markers.
  714. */
  715. public void removeAllMarkers() {
  716. Integer[] ids = this.markers.keySet().toArray(new Integer[this.markers.size()]);
  717. for (Integer id : ids) {
  718. removeMarker(id);
  719. }
  720. }
  721.  
  722. private void addMarker(int id, AceRange range) {
  723. markers.put(id, range);
  724. }
  725.  
  726. private void removeRegisteredMarker(int id) {
  727. AceRange range = markers.remove(id);
  728. range.detach();
  729. }
  730.  
  731. /**
  732. * Prepare a wrapper around Ace Selection object.
  733. * @return a wrapper around Ace Selection object
  734. */
  735. public AceSelection getSelection() {
  736. if (selection == null)
  737. selection = new AceSelection(getSelectionJS());
  738. return selection;
  739. }
  740.  
  741. private native JavaScriptObject getSelectionJS() /*-{
  742. var editor = this.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  743. return editor.getSession().getSelection();
  744. }-*/;
  745.  
  746. /**
  747. * Bind command line and editor. For default implementation of command line
  748. * you can use <code> AceCommandLine cmdLine = new AceDefaultCommandLine(textBox) </code>
  749. * where textBox could be for instance standard GWT TextBox or TextArea.
  750. * @param cmdLine implementation of command line
  751. */
  752. public void initializeCommandLine(AceCommandLine cmdLine) {
  753. this.commandLine = cmdLine;
  754. this.commandLine.setCommandLineListener(new AceCommandLineListener() {
  755. @Override
  756. public void onCommandEntered(String command) {
  757. execCommand(command);
  758. }
  759. });
  760. }
  761.  
  762. private static AceCompletionCallback wrapCompletionCallback(JavaScriptObject jsCallback) {
  763.  
  764. return new AceCompletionCallbackImpl(jsCallback);
  765. }
  766. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement