PageRenderTime 67ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 98 lines | 60 code | 16 blank | 22 comment | 8 complexity | 5b08dd4ae6b3f14382593af0da50b7be 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. * CharCaretListener.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.utils;
  21. import org.gjt.sp.jedit.textarea.JEditTextArea;
  22. import org.jedit.ruby.structure.RubyTokenHandler;
  23. import org.jedit.ruby.structure.RubyToken;
  24. import org.jedit.ruby.structure.BufferChangeHandler;
  25. import javax.swing.event.CaretListener;
  26. import javax.swing.event.CaretEvent;
  27. /**
  28. * @author robmckinnon at users.sourceforge.net
  29. */
  30. public final class CharCaretListener implements CaretListener {
  31. private static final RubyTokenHandler tokenHandler = new RubyTokenHandler();
  32. private static final char NONE = (char)-1;
  33. private static RubyToken lastToken = null;
  34. private static RubyToken currentToken = null;
  35. private static char charLastBehind = NONE;
  36. private static char charBehind = NONE;
  37. private static char charAhead = NONE;
  38. public static boolean hasCharLastBehind() {
  39. return charLastBehind != NONE;
  40. }
  41. public static char getCharBehind() {
  42. return charBehind;
  43. }
  44. public static char getCharLastBehind() {
  45. return charLastBehind;
  46. }
  47. public static char getCharAhead() {
  48. return charAhead;
  49. }
  50. public static RubyToken getLastToken() {
  51. return lastToken;
  52. }
  53. public static RubyToken getCurrentToken() {
  54. return currentToken;
  55. }
  56. public final void caretUpdate(CaretEvent e) {
  57. if (!BufferChangeHandler.instance().isGotoPreviousEditAction()) {
  58. BufferChangeHandler.instance().resetEditLocationIndex();
  59. }
  60. int mark = e.getMark();
  61. int dot = e.getDot();
  62. charLastBehind = charBehind;
  63. lastToken = currentToken;
  64. if (dot == mark && dot > 0) {
  65. JEditTextArea textArea = (JEditTextArea)e.getSource();
  66. String text = textArea.getText();
  67. charBehind = text.charAt(dot - 1);
  68. currentToken = tokenHandler.getTokenAtCaret(CommandUtils.getBuffer(textArea), dot);
  69. if (text.length() > dot) {
  70. charAhead = text.charAt(dot);
  71. } else {
  72. charAhead = NONE;
  73. }
  74. } else {
  75. currentToken = null;
  76. lastToken = null;
  77. charBehind = NONE;
  78. charLastBehind = NONE;
  79. charAhead = NONE;
  80. }
  81. }
  82. }