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

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/indent/WhitespaceRule.java

#
Java | 80 lines | 32 code | 8 blank | 40 comment | 6 complexity | 5cbd1ed845372e5bd12f5f615f83fee9 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. * WhitespaceRule.java
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2007 Marcelo Vanzin
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.indent;
  23. import java.util.List;
  24. import org.gjt.sp.jedit.buffer.JEditBuffer;
  25. /**
  26. * Whitespace rule. This rule cancels all indent actions in the
  27. * following case:
  28. *
  29. * <ul>
  30. * <li>The previous line is all whitespace</li>
  31. * <li>The current line is not empty</li>
  32. * </ul>
  33. *
  34. * <p>The result is that this rule won't allow the indentation to be
  35. * increased, only decreased (by rules triggered by unindentThisLine).
  36. * If the requirements above do not apply, this rule does nothing.</p>
  37. *
  38. * @author Marcelo Vanzin
  39. * @version $Id: WhitespaceRule.java 15614 2009-06-30 07:21:56Z voituk $
  40. * @since jEdit 4.3pre10
  41. */
  42. public class WhitespaceRule implements IndentRule
  43. {
  44. public void apply(JEditBuffer buffer, int thisLineIndex,
  45. int prevLineIndex, int prevPrevLineIndex,
  46. List<IndentAction> indentActions)
  47. {
  48. /* Don't apply this rule if the current line is empty. */
  49. CharSequence current = buffer.getLineSegment(thisLineIndex);
  50. boolean found = false;
  51. for (int i = 0; i < current.length(); i++)
  52. {
  53. if (!Character.isWhitespace(current.charAt(i)))
  54. {
  55. found = true;
  56. break;
  57. }
  58. }
  59. if (!found)
  60. return;
  61. /* Check if the previous line is empty. */
  62. if (prevLineIndex >= 0) {
  63. CharSequence previous = buffer.getLineSegment(prevLineIndex);
  64. for (int i = 0; i < previous.length(); i++)
  65. {
  66. if (!Character.isWhitespace(previous.charAt(i)))
  67. return;
  68. }
  69. }
  70. indentActions.add(new IndentAction.NoIncrease());
  71. }
  72. }