/org.golang.godev/src/org/golang/godev/editors/NonRuleBasedDamagerRepairer.java

http://godev-projects.googlecode.com/ · Java · 138 lines · 89 code · 18 blank · 31 comment · 7 complexity · 33e6a9ee20bdc46560b8c2f9287499f6 MD5 · raw file

  1. package org.golang.godev.editors;
  2. import org.eclipse.jface.text.BadLocationException;
  3. import org.eclipse.jface.text.DocumentEvent;
  4. import org.eclipse.jface.text.IDocument;
  5. import org.eclipse.jface.text.IRegion;
  6. import org.eclipse.jface.text.ITypedRegion;
  7. import org.eclipse.jface.text.Region;
  8. import org.eclipse.jface.text.TextAttribute;
  9. import org.eclipse.jface.text.TextPresentation;
  10. import org.eclipse.jface.text.presentation.IPresentationDamager;
  11. import org.eclipse.jface.text.presentation.IPresentationRepairer;
  12. import org.eclipse.core.runtime.Assert;
  13. import org.eclipse.swt.custom.StyleRange;
  14. public class NonRuleBasedDamagerRepairer
  15. implements IPresentationDamager, IPresentationRepairer {
  16. /** The document this object works on */
  17. protected IDocument fDocument;
  18. /** The default text attribute if non is returned as data by the current token */
  19. protected TextAttribute fDefaultTextAttribute;
  20. /**
  21. * Constructor for NonRuleBasedDamagerRepairer.
  22. */
  23. public NonRuleBasedDamagerRepairer(TextAttribute defaultTextAttribute) {
  24. Assert.isNotNull(defaultTextAttribute);
  25. fDefaultTextAttribute = defaultTextAttribute;
  26. }
  27. /**
  28. * @see IPresentationRepairer#setDocument(IDocument)
  29. */
  30. public void setDocument(IDocument document) {
  31. fDocument = document;
  32. }
  33. /**
  34. * Returns the end offset of the line that contains the specified offset or
  35. * if the offset is inside a line delimiter, the end offset of the next line.
  36. *
  37. * @param offset the offset whose line end offset must be computed
  38. * @return the line end offset for the given offset
  39. * @exception BadLocationException if offset is invalid in the current document
  40. */
  41. protected int endOfLineOf(int offset) throws BadLocationException {
  42. IRegion info = fDocument.getLineInformationOfOffset(offset);
  43. if (offset <= info.getOffset() + info.getLength())
  44. return info.getOffset() + info.getLength();
  45. int line = fDocument.getLineOfOffset(offset);
  46. try {
  47. info = fDocument.getLineInformation(line + 1);
  48. return info.getOffset() + info.getLength();
  49. } catch (BadLocationException x) {
  50. return fDocument.getLength();
  51. }
  52. }
  53. /**
  54. * @see IPresentationDamager#getDamageRegion(ITypedRegion, DocumentEvent, boolean)
  55. */
  56. public IRegion getDamageRegion(
  57. ITypedRegion partition,
  58. DocumentEvent event,
  59. boolean documentPartitioningChanged) {
  60. if (!documentPartitioningChanged) {
  61. try {
  62. IRegion info =
  63. fDocument.getLineInformationOfOffset(event.getOffset());
  64. int start = Math.max(partition.getOffset(), info.getOffset());
  65. int end =
  66. event.getOffset()
  67. + (event.getText() == null
  68. ? event.getLength()
  69. : event.getText().length());
  70. if (info.getOffset() <= end
  71. && end <= info.getOffset() + info.getLength()) {
  72. // optimize the case of the same line
  73. end = info.getOffset() + info.getLength();
  74. } else
  75. end = endOfLineOf(end);
  76. end =
  77. Math.min(
  78. partition.getOffset() + partition.getLength(),
  79. end);
  80. return new Region(start, end - start);
  81. } catch (BadLocationException x) {
  82. }
  83. }
  84. return partition;
  85. }
  86. /**
  87. * @see IPresentationRepairer#createPresentation(TextPresentation, ITypedRegion)
  88. */
  89. public void createPresentation(
  90. TextPresentation presentation,
  91. ITypedRegion region) {
  92. addRange(
  93. presentation,
  94. region.getOffset(),
  95. region.getLength(),
  96. fDefaultTextAttribute);
  97. }
  98. /**
  99. * Adds style information to the given text presentation.
  100. *
  101. * @param presentation the text presentation to be extended
  102. * @param offset the offset of the range to be styled
  103. * @param length the length of the range to be styled
  104. * @param attr the attribute describing the style of the range to be styled
  105. */
  106. protected void addRange(
  107. TextPresentation presentation,
  108. int offset,
  109. int length,
  110. TextAttribute attr) {
  111. if (attr != null)
  112. presentation.addStyleRange(
  113. new StyleRange(
  114. offset,
  115. length,
  116. attr.getForeground(),
  117. attr.getBackground(),
  118. attr.getStyle()));
  119. }
  120. }