PageRenderTime 31ms CodeModel.GetById 21ms app.highlight 7ms RepoModel.GetById 2ms app.codeStats 0ms

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

#
Java | 90 lines | 54 code | 14 blank | 22 comment | 11 complexity | 3edd889e4a9df26469c82f97b95f07e6 MD5 | raw file
 1/*
 2 * RubyStructureMatcher.java - 
 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
21package org.jedit.ruby.structure;
22
23import org.gjt.sp.jedit.textarea.StructureMatcher;
24import org.gjt.sp.jedit.textarea.TextArea;
25import org.jedit.ruby.RubyPlugin;
26import org.jedit.ruby.utils.EditorView;
27import org.jedit.ruby.ast.Member;
28import org.jedit.ruby.ast.MemberVisitorAdapter;
29import org.jedit.ruby.ast.Root;
30
31/**
32 * @author robmckinnon at users.sourceforge.net
33 */
34public final class RubyStructureMatcher extends MemberVisitorAdapter implements StructureMatcher {
35
36    private Match match;
37
38    public final Match getMatch(TextArea textArea) {
39        if (isRuby(textArea)) {
40            match = null;
41            EditorView view = RubyPlugin.getActiveView();
42            Member member = view.getMemberAtCaretPosition();
43
44            if (member != null) {
45                member.accept(this);
46            }
47            return match;
48        } else {
49            return null;
50        }
51    }
52
53    public final void handleDefault(Member member) {
54        EditorView view = RubyPlugin.getActiveView();
55        int position = view.getCaretPosition();
56        int start = member.getStartOuterOffset();
57        int startInner = member.getStartOffset();
58        int nameEnd = startInner + member.getCompositeName().length();
59        int end = member.getEndOffset();
60
61        boolean endIsPresent = end <= view.getTextLength() && view.getText(end - 3, 3).equals("end");
62
63        if (endIsPresent) {
64            boolean showStart = position > end - 4;
65            boolean showEnd = position >= start && position <= startInner - 1;
66
67            if (showStart) {
68                int startLine = view.getLineAtOffset(start);
69                match = new Match(this, startLine, start, view.getLineAtOffset(nameEnd), nameEnd);
70            } else if (showEnd) {
71                int endLine = view.getLineAtOffset(end);
72                match = new Match(this, endLine, end - 3, endLine, end);
73            }
74        }
75    }
76
77    public final void handleRoot(Root root) {
78    }
79
80
81    public final void selectMatch(TextArea textArea) {
82        if (isRuby(textArea)) {
83            RubyPlugin.log("Select match", getClass());
84        }
85    }
86
87    private static boolean isRuby(TextArea textArea) {
88        return RubyPlugin.isRuby(textArea);
89    }
90}