PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bundles/plugins-trunk/RubyPlugin/src/org/jedit/ruby/RubyActions.java

#
Java | 292 lines | 227 code | 38 blank | 27 comment | 42 complexity | 6ecbebcce92ab8dd32d697883ba28a73 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * RubyActions.java - Actions for Ruby plugin
  3. *
  4. * Copyright 2005 Robert McKinnon
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.jedit.ruby;
  21. import errorlist.ErrorSource;
  22. import org.gjt.sp.jedit.*;
  23. import org.gjt.sp.jedit.gui.DockableWindowManager;
  24. import org.gjt.sp.jedit.textarea.JEditTextArea;
  25. import org.jedit.ruby.ast.Member;
  26. import org.jedit.ruby.ast.Method;
  27. import org.jedit.ruby.ast.RubyMembers;
  28. import org.jedit.ruby.parser.RubyParser;
  29. import org.jedit.ruby.structure.RubySideKickParser;
  30. import org.jedit.ruby.structure.*;
  31. import org.jedit.ruby.structure.TypeAheadPopup;
  32. import org.jedit.ruby.structure.FileStructurePopup;
  33. import org.jedit.ruby.structure.AutoIndentAndInsertEnd;
  34. import org.jedit.ruby.cache.*;
  35. import org.jedit.ruby.ri.*;
  36. import org.jedit.ruby.utils.CommandUtils;
  37. import java.util.*;
  38. import java.awt.datatransfer.StringSelection;
  39. import java.io.IOException;
  40. import sidekick.SideKickActions;
  41. /**
  42. * @author robmckinnon at users,sourceforge,net
  43. */
  44. public final class RubyActions {
  45. public static void progressiveSelection(View view) {
  46. ProgressiveSelector.doProgressiveSelection(view);
  47. }
  48. public static void pasteLine(View view, JEditTextArea textArea) {
  49. if (!view.getBuffer().isReadOnly()) {
  50. if (isLineInClipBoard() && textArea.getSelectionCount() == 0) {
  51. int lineLength = jEdit.getIntegerProperty("ruby.clipboard-line-length", 0);
  52. String clipBoardText = Registers.getRegister('$').toString();
  53. if (clipBoardText.length() == lineLength) {
  54. int position = textArea.getCaretPosition();
  55. int line = textArea.getCaretLine();
  56. int startOffset = textArea.getLineStartOffset(line);
  57. view.getBuffer().insert(startOffset, clipBoardText);
  58. textArea.setCaretPosition(position + clipBoardText.length());
  59. } else {
  60. setLineInClipBoard(false);
  61. pasteFromClipBoard(textArea);
  62. }
  63. } else {
  64. pasteFromClipBoard(textArea);
  65. }
  66. }
  67. }
  68. public static void copyLine(JEditTextArea textArea) {
  69. if (textArea.getSelectionCount() > 0) {
  70. copySelectionToClipBoard(textArea);
  71. } else {
  72. putLineInClipBoard(textArea);
  73. }
  74. }
  75. public static void cutLine(JEditTextArea textArea) {
  76. if (textArea.getSelectionCount() > 0) {
  77. cutSelectionToClipBoard(textArea);
  78. } else {
  79. putLineInClipBoard(textArea);
  80. textArea.deleteLine();
  81. }
  82. }
  83. private static void putLineInClipBoard(JEditTextArea textArea) {
  84. int line = textArea.getCaretLine();
  85. String text = textArea.getLineText(line) + jEdit.getProperty("buffer.lineSeparator");
  86. Registers.getRegister('$').setTransferable( new StringSelection(text) );
  87. jEdit.setProperty("ruby.clipboard-line-length", Integer.toString(text.length()));
  88. setLineInClipBoard(true);
  89. }
  90. private static void copySelectionToClipBoard(JEditTextArea textArea) {
  91. Registers.copy(textArea, '$');
  92. setLineInClipBoard(false);
  93. }
  94. private static void cutSelectionToClipBoard(JEditTextArea textArea) {
  95. Registers.cut(textArea, '$');
  96. setLineInClipBoard(false);
  97. }
  98. private static void pasteFromClipBoard(JEditTextArea textArea) {
  99. Registers.paste(textArea, '$');
  100. }
  101. private static boolean isLineInClipBoard() {
  102. return jEdit.getBooleanProperty("ruby.line-in-clipboard", false);
  103. }
  104. private static void setLineInClipBoard(boolean isLine) {
  105. jEdit.setProperty("ruby.line-in-clipboard", Boolean.valueOf(isLine).toString());
  106. }
  107. public static void introduceVariable(View view) {
  108. Debug.DUMP_KEY_EVENTS = true;
  109. String prompt = jEdit.getProperty("ruby.introduce-variable.message");
  110. String name = Macros.input(view, prompt);
  111. if (name != null && name.length() > 0) {
  112. JEditTextArea textArea = view.getTextArea();
  113. cutSelectionToClipBoard(textArea);
  114. textArea.setSelectedText(name);
  115. textArea.goToPrevLine(false);
  116. textArea.goToEndOfWhiteSpace(false);
  117. RubyActions.autoIndentAndInsertEnd(view);
  118. textArea.setSelectedText(name + " = ");
  119. pasteFromClipBoard(textArea);
  120. textArea.goToNextLine(false);
  121. }
  122. }
  123. public static void searchDocumentation(View view) {
  124. try {
  125. if (CommandUtils.isRubyInstalled()) {
  126. RDocSeacher.doSearch(view);
  127. }
  128. } catch (IOException e) {
  129. e.printStackTrace();
  130. } catch (InterruptedException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134. public static void findDeclaration(View view) {
  135. JEditTextArea textArea = view.getTextArea();
  136. int caretPosition = textArea.getCaretPosition();
  137. textArea.selectWord();
  138. String text = textArea.getSelectedText();
  139. textArea.setCaretPosition(caretPosition);
  140. RubyPlugin.log("looking for methods named: " + text, RubyActions.class);
  141. List<Method> methods = RubyCache.instance().getMethods(text);
  142. RubyPlugin.log("found: " + methods.size(), RubyActions.class);
  143. if (methods.size() > 0) {
  144. Member[] displayMembers = methods.toArray(new Member[methods.size()]);
  145. new TypeAheadPopup(view, displayMembers, displayMembers[0], org.jedit.ruby.structure.TypeAheadPopup.FIND_DECLARATION_POPUP);
  146. } else {
  147. Macros.message(textArea, jEdit.getProperty("ruby.find-declaration.no-matches.label"));
  148. }
  149. }
  150. public static void fileStructurePopup(View view) {
  151. FileStructurePopup fileStructurePopup = new FileStructurePopup(view);
  152. fileStructurePopup.show();
  153. }
  154. public static void toggleSpec(View view) {
  155. SpecToggler specToggler = new SpecToggler(view);
  156. specToggler.toggleSpec();
  157. }
  158. public static void structureBrowser(View view) {
  159. DockableWindowManager windowManager = view.getDockableWindowManager();
  160. windowManager.showDockableWindow("sidekick-tree");
  161. }
  162. public static void autoIndentAndInsertEnd(View view) {
  163. if (isRubyFile(view)) {
  164. // start = System.currentTimeMillis();
  165. AutoIndentAndInsertEnd.performIndent(view);
  166. // end = System.currentTimeMillis();
  167. // Macros.message(view, "" + (end - start));
  168. } else {
  169. view.getTextArea().insertEnterAndIndent();
  170. }
  171. }
  172. public static void nextMethod(View view) {
  173. if (isRubyFile(view)) {
  174. RubyMembers members = RubyParser.getMembers(view);
  175. if (!members.containsErrors()) {
  176. JEditTextArea textArea = view.getTextArea();
  177. int caretPosition = textArea.getCaretPosition();
  178. Member member = members.getNextMember(caretPosition);
  179. if (member != null) {
  180. textArea.setCaretPosition(member.getStartOffset(), true);
  181. } else {
  182. textArea.setCaretPosition(textArea.getBufferLength(), true);
  183. }
  184. }
  185. } else {
  186. try {
  187. SideKickActions.goToNextAsset(view);
  188. } catch (IllegalArgumentException e) {
  189. // bug in SideKick
  190. JEditTextArea textArea = view.getTextArea();
  191. textArea.goToNextLine(false);
  192. }
  193. }
  194. }
  195. public static void previousMethod(View view) {
  196. if (isRubyFile(view)) {
  197. RubyMembers members = RubyParser.getMembers(view);
  198. if (!members.containsErrors()) {
  199. JEditTextArea textArea = view.getTextArea();
  200. int caretPosition = textArea.getCaretPosition();
  201. Member member = members.getLastMemberBefore(caretPosition);
  202. if (member != null && caretPosition == member.getStartOffset()) {
  203. member = members.getPreviousMember(caretPosition);
  204. }
  205. if (member != null) {
  206. textArea.setCaretPosition(member.getStartOffset(), true);
  207. } else {
  208. textArea.setCaretPosition(0, true);
  209. }
  210. }
  211. } else {
  212. try {
  213. SideKickActions.goToPrevAsset(view);
  214. } catch (NullPointerException e) {
  215. // bug in SideKick, do nothing
  216. }
  217. }
  218. }
  219. public static void previousEdit(View view) {
  220. BufferChangeHandler.instance().gotoPreviousEdit(view);
  221. }
  222. public static void nextError(View view) {
  223. if (isRubyFile(view)) {
  224. JEditTextArea textArea = view.getTextArea();
  225. int caretPosition = textArea.getCaretPosition();
  226. ErrorSource.Error[] errors = org.jedit.ruby.structure.RubySideKickParser.getErrors();
  227. for (ErrorSource.Error error : errors) {
  228. int offset = RubyPlugin.getNonSpaceStartOffset(error.getLineNumber());
  229. if (caretPosition < offset) {
  230. textArea.setCaretPosition(offset, true);
  231. break;
  232. }
  233. }
  234. }
  235. }
  236. public static void previousError(View view) {
  237. if (isRubyFile(view)) {
  238. JEditTextArea textArea = view.getTextArea();
  239. int caretPosition = textArea.getCaretPosition();
  240. List<ErrorSource.Error> errors = Arrays.asList(RubySideKickParser.getErrors());
  241. Collections.reverse(errors);
  242. for (ErrorSource.Error error : errors) {
  243. int offset = RubyPlugin.getNonSpaceStartOffset(error.getLineNumber());
  244. if (caretPosition > offset) {
  245. textArea.setCaretPosition(offset, true);
  246. break;
  247. }
  248. }
  249. }
  250. }
  251. private static boolean isRubyFile(View view) {
  252. return RubyPlugin.isRuby(view.getBuffer());
  253. }
  254. }