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