PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/initial/nof/incubator/ide/trunk/com.cb.eclipse.folding/src/com/cb/eclipse/folding/java/calculation/CommentHelper.java

#
Java | 167 lines | 92 code | 35 blank | 40 comment | 11 complexity | 84e10043de55b02e7e192fb78d898a67 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, LGPL-2.1, BSD-3-Clause, Apache-2.0, GPL-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /*******************************************************************************
  20. * Copyright (c) 2004 Coffee-Bytes.com and others.
  21. * All rights reserved. This program and the accompanying materials
  22. * are made available under the terms of the Common Public License v1.0
  23. * which accompanies this distribution, and is available at
  24. * http://www.opensource.org/licenses/cpl.php
  25. *
  26. * Contributors:
  27. * Coffee-Bytes.com - initial API and implementation
  28. *******************************************************************************/
  29. package com.cb.eclipse.folding.java.calculation;
  30. import java.util.HashSet;
  31. import java.util.Set;
  32. import org.eclipse.jdt.core.IJavaElement;
  33. import org.eclipse.jdt.core.JavaModelException;
  34. import org.eclipse.jdt.core.compiler.ITerminalSymbols;
  35. import com.cb.eclipse.folding.EnhancedPosition;
  36. import com.cb.eclipse.folding.EnhancedPosition2;
  37. import com.cb.eclipse.folding.java.JavaPositionMetadata;
  38. /**
  39. * The CommentHelper class is just a common location for the core logic of
  40. * processing comments and producing comment folding regions.
  41. *
  42. * One of the key components of the comment helper class is the comment folding
  43. * advisor, which is the client provided class the provides the knowledge of
  44. * whether certain regions should be foldable and/or collapsed initially.
  45. * Because the comment helper class is generic in nature, the advisor allows for
  46. * pluggable scenarios.
  47. *
  48. * @author R.J. Lorimer
  49. */
  50. @SuppressWarnings({"unused", "unchecked"})
  51. public class CommentHelper {
  52. private Set regions = new HashSet();
  53. private int lineCommentCount;
  54. private int lineCommentStart;
  55. private int lineCommentEnd;
  56. private CommentFoldingAdvisor advisor;
  57. private UserDefinedRegionHelper userDefinedRecognizer;
  58. public CommentHelper() {
  59. this(DefaultCommentFoldingAdvisor.getInstance());
  60. }
  61. public CommentHelper(CommentFoldingAdvisor advisor) {
  62. this.advisor = advisor;
  63. userDefinedRecognizer = new UserDefinedRegionHelper();
  64. }
  65. public void handle(int token, int start, int end, IJavaElement owner) throws JavaModelException {
  66. switch(token) {
  67. case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
  68. handleCommentBlock(start, end);
  69. break;
  70. case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
  71. handleJavadoc(start, end, owner);
  72. break;
  73. default:
  74. if(ITerminalSymbols.TokenNameCOMMENT_LINE == token && !isUserDefinedSentinel(token, start, end, owner)) {
  75. handleCommentLine(start, end);
  76. }
  77. else {
  78. handleNonCommentToken(token);
  79. }
  80. break;
  81. }
  82. }
  83. public void end() {
  84. closeOpenComments();
  85. }
  86. public Set result() {
  87. return regions;
  88. }
  89. public void reset() {
  90. regions.clear();
  91. }
  92. private boolean isUserDefinedSentinel(int token, int start, int end, IJavaElement owner) throws JavaModelException {
  93. return userDefinedRecognizer.isOpeningSentinel(start, end, owner) || userDefinedRecognizer.isClosingSentinel(start, end, owner);
  94. }
  95. private void handleCommentLine(int startPos, int endPos) {
  96. if (lineCommentCount == 0) {
  97. lineCommentStart = startPos;
  98. }
  99. lineCommentEnd = endPos;
  100. lineCommentCount++;
  101. }
  102. private void handleCommentBlock(int start, int end) {
  103. if (advisor.shouldFoldBlockComment()) {
  104. boolean collapse = advisor.shouldCollapseBlockComment();
  105. regions.add(new EnhancedPosition2(start, end - start, getMetadata(collapse)));
  106. }
  107. }
  108. private void handleJavadoc(int start, int end, IJavaElement owner) {
  109. if (advisor.shouldFoldJavadoc()) {
  110. boolean collapse = advisor.shouldCollapseJavadoc();
  111. regions.add(new EnhancedPosition2(start, end - start, getMetadata(collapse)));
  112. }
  113. }
  114. private void handleNonCommentToken(int token) {
  115. closeOpenComments();
  116. }
  117. private void closeOpenComments() {
  118. if (lineCommentCount > 1) {
  119. if (advisor.shouldFoldLineComment()) {
  120. boolean collapse = advisor.shouldCollapseLineComment();
  121. regions.add(new EnhancedPosition(lineCommentStart, lineCommentEnd - lineCommentStart - 1, getMetadata(collapse)));
  122. }
  123. }
  124. lineCommentCount = 0;
  125. }
  126. public String toString() {
  127. return "Comment Helper - regions: " + result().size();
  128. }
  129. private JavaPositionMetadata getMetadata(boolean collapse) {
  130. return new JavaPositionMetadata(false, false, collapse, false, null);
  131. }
  132. }