PageRenderTime 141ms CodeModel.GetById 113ms app.highlight 24ms RepoModel.GetById 1ms 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
  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 */
 20package org.jedit.ruby;
 21
 22import errorlist.ErrorSource;
 23import org.gjt.sp.jedit.*;
 24import org.gjt.sp.jedit.gui.DockableWindowManager;
 25import org.gjt.sp.jedit.textarea.JEditTextArea;
 26import org.jedit.ruby.ast.Member;
 27import org.jedit.ruby.ast.Method;
 28import org.jedit.ruby.ast.RubyMembers;
 29import org.jedit.ruby.parser.RubyParser;
 30import org.jedit.ruby.structure.RubySideKickParser;
 31import org.jedit.ruby.structure.*;
 32import org.jedit.ruby.structure.TypeAheadPopup;
 33import org.jedit.ruby.structure.FileStructurePopup;
 34import org.jedit.ruby.structure.AutoIndentAndInsertEnd;
 35import org.jedit.ruby.cache.*;
 36import org.jedit.ruby.ri.*;
 37import org.jedit.ruby.utils.CommandUtils;
 38
 39import java.util.*;
 40import java.awt.datatransfer.StringSelection;
 41import java.io.IOException;
 42
 43import sidekick.SideKickActions;
 44
 45/**
 46 * @author robmckinnon at users,sourceforge,net
 47 */
 48public final class RubyActions {
 49
 50    public static void progressiveSelection(View view) {
 51        ProgressiveSelector.doProgressiveSelection(view);
 52    }
 53
 54    public static void pasteLine(View view, JEditTextArea textArea) {
 55        if (!view.getBuffer().isReadOnly()) {
 56            if (isLineInClipBoard() && textArea.getSelectionCount() == 0) {
 57                int lineLength = jEdit.getIntegerProperty("ruby.clipboard-line-length", 0);
 58                String clipBoardText = Registers.getRegister('$').toString();
 59
 60                if (clipBoardText.length() == lineLength) {
 61                    int position = textArea.getCaretPosition();
 62                    int line = textArea.getCaretLine();
 63                    int startOffset = textArea.getLineStartOffset(line);
 64                    view.getBuffer().insert(startOffset, clipBoardText);
 65                    textArea.setCaretPosition(position + clipBoardText.length());
 66                } else {
 67                    setLineInClipBoard(false);
 68                    pasteFromClipBoard(textArea);
 69                }
 70            } else {
 71                pasteFromClipBoard(textArea);
 72            }
 73        }
 74    }
 75
 76    public static void copyLine(JEditTextArea textArea) {
 77        if (textArea.getSelectionCount() > 0) {
 78            copySelectionToClipBoard(textArea);
 79        } else {
 80            putLineInClipBoard(textArea);
 81        }
 82    }
 83
 84    public static void cutLine(JEditTextArea textArea) {
 85        if (textArea.getSelectionCount() > 0) {
 86            cutSelectionToClipBoard(textArea);
 87        } else {
 88            putLineInClipBoard(textArea);
 89            textArea.deleteLine();
 90        }
 91    }
 92
 93    private static void putLineInClipBoard(JEditTextArea textArea) {
 94        int line = textArea.getCaretLine();
 95        String text = textArea.getLineText(line) + jEdit.getProperty("buffer.lineSeparator");
 96        Registers.getRegister('$').setTransferable( new StringSelection(text) );
 97        jEdit.setProperty("ruby.clipboard-line-length", Integer.toString(text.length()));
 98        setLineInClipBoard(true);
 99    }
100
101    private static void copySelectionToClipBoard(JEditTextArea textArea) {
102        Registers.copy(textArea, '$');
103        setLineInClipBoard(false);
104    }
105
106    private static void cutSelectionToClipBoard(JEditTextArea textArea) {
107        Registers.cut(textArea, '$');
108        setLineInClipBoard(false);
109    }
110
111    private static void pasteFromClipBoard(JEditTextArea textArea) {
112        Registers.paste(textArea, '$');
113    }
114
115    private static boolean isLineInClipBoard() {
116        return jEdit.getBooleanProperty("ruby.line-in-clipboard", false);
117    }
118
119    private static void setLineInClipBoard(boolean isLine) {
120        jEdit.setProperty("ruby.line-in-clipboard", Boolean.valueOf(isLine).toString());
121    }
122
123    public static void introduceVariable(View view) {
124        Debug.DUMP_KEY_EVENTS = true;
125
126        String prompt = jEdit.getProperty("ruby.introduce-variable.message");
127        String name = Macros.input(view, prompt);
128
129        if (name != null && name.length() > 0) {
130            JEditTextArea textArea = view.getTextArea();
131            cutSelectionToClipBoard(textArea);
132            textArea.setSelectedText(name);
133            textArea.goToPrevLine(false);
134            textArea.goToEndOfWhiteSpace(false);
135            RubyActions.autoIndentAndInsertEnd(view);
136            textArea.setSelectedText(name + " = ");
137            pasteFromClipBoard(textArea);
138            textArea.goToNextLine(false);
139        }
140    }
141
142    public static void searchDocumentation(View view) {
143        try {
144            if (CommandUtils.isRubyInstalled()) {
145                RDocSeacher.doSearch(view);
146            }
147        } catch (IOException e) {
148            e.printStackTrace();
149        } catch (InterruptedException e) {
150            e.printStackTrace();
151        }
152    }
153
154    public static void findDeclaration(View view) {
155        JEditTextArea textArea = view.getTextArea();
156        int caretPosition = textArea.getCaretPosition();
157        textArea.selectWord();
158        String text = textArea.getSelectedText();
159        textArea.setCaretPosition(caretPosition);
160
161        RubyPlugin.log("looking for methods named: " + text, RubyActions.class);
162        List<Method> methods = RubyCache.instance().getMethods(text);
163        RubyPlugin.log("found: " + methods.size(), RubyActions.class);
164
165        if (methods.size() > 0) {
166            Member[] displayMembers = methods.toArray(new Member[methods.size()]);
167            new TypeAheadPopup(view, displayMembers, displayMembers[0], org.jedit.ruby.structure.TypeAheadPopup.FIND_DECLARATION_POPUP);
168        } else {
169            Macros.message(textArea, jEdit.getProperty("ruby.find-declaration.no-matches.label"));
170        }
171    }
172
173    public static void fileStructurePopup(View view) {
174        FileStructurePopup fileStructurePopup = new FileStructurePopup(view);
175        fileStructurePopup.show();
176    }
177
178    public static void toggleSpec(View view) {
179        SpecToggler specToggler = new SpecToggler(view);
180        specToggler.toggleSpec();
181    }
182
183    public static void structureBrowser(View view) {
184        DockableWindowManager windowManager = view.getDockableWindowManager();
185        windowManager.showDockableWindow("sidekick-tree");
186    }
187
188    public static void autoIndentAndInsertEnd(View view) {
189        if (isRubyFile(view)) {
190// start = System.currentTimeMillis();
191            AutoIndentAndInsertEnd.performIndent(view);
192// end = System.currentTimeMillis();
193// Macros.message(view, "" + (end - start));
194        } else {
195            view.getTextArea().insertEnterAndIndent();
196        }
197    }
198
199    public static void nextMethod(View view) {
200        if (isRubyFile(view)) {
201            RubyMembers members = RubyParser.getMembers(view);
202
203            if (!members.containsErrors()) {
204                JEditTextArea textArea = view.getTextArea();
205                int caretPosition = textArea.getCaretPosition();
206                Member member = members.getNextMember(caretPosition);
207
208                if (member != null) {
209                    textArea.setCaretPosition(member.getStartOffset(), true);
210                } else {
211                    textArea.setCaretPosition(textArea.getBufferLength(), true);
212                }
213            }
214        } else {
215            try {
216                SideKickActions.goToNextAsset(view);
217            } catch (IllegalArgumentException e) {
218                // bug in SideKick
219                JEditTextArea textArea = view.getTextArea();
220                textArea.goToNextLine(false);
221            }
222        }
223    }
224
225    public static void previousMethod(View view) {
226        if (isRubyFile(view)) {
227            RubyMembers members = RubyParser.getMembers(view);
228
229            if (!members.containsErrors()) {
230                JEditTextArea textArea = view.getTextArea();
231                int caretPosition = textArea.getCaretPosition();
232                Member member = members.getLastMemberBefore(caretPosition);
233                if (member != null && caretPosition == member.getStartOffset()) {
234                    member = members.getPreviousMember(caretPosition);
235                }
236
237                if (member != null) {
238                    textArea.setCaretPosition(member.getStartOffset(), true);
239                } else {
240                    textArea.setCaretPosition(0, true);
241                }
242            }
243        } else {
244            try {
245                SideKickActions.goToPrevAsset(view);
246            } catch (NullPointerException e) {
247                // bug in SideKick, do nothing
248            }
249        }
250    }
251
252    public static void previousEdit(View view) {
253        BufferChangeHandler.instance().gotoPreviousEdit(view);
254    }
255
256    public static void nextError(View view) {
257        if (isRubyFile(view)) {
258            JEditTextArea textArea = view.getTextArea();
259            int caretPosition = textArea.getCaretPosition();
260            ErrorSource.Error[] errors = org.jedit.ruby.structure.RubySideKickParser.getErrors();
261
262            for (ErrorSource.Error error : errors) {
263                int offset = RubyPlugin.getNonSpaceStartOffset(error.getLineNumber());
264                if (caretPosition < offset) {
265                    textArea.setCaretPosition(offset, true);
266                    break;
267                }
268            }
269        }
270    }
271
272    public static void previousError(View view) {
273        if (isRubyFile(view)) {
274            JEditTextArea textArea = view.getTextArea();
275            int caretPosition = textArea.getCaretPosition();
276            List<ErrorSource.Error> errors = Arrays.asList(RubySideKickParser.getErrors());
277            Collections.reverse(errors);
278
279            for (ErrorSource.Error error : errors) {
280                int offset = RubyPlugin.getNonSpaceStartOffset(error.getLineNumber());
281                if (caretPosition > offset) {
282                    textArea.setCaretPosition(offset, true);
283                    break;
284                }
285            }
286        }
287    }
288
289    private static boolean isRubyFile(View view) {
290        return RubyPlugin.isRuby(view.getBuffer());
291    }
292}