/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
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
- /*******************************************************************************
- * Copyright (c) 2004 Coffee-Bytes.com and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Common Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.opensource.org/licenses/cpl.php
- *
- * Contributors:
- * Coffee-Bytes.com - initial API and implementation
- *******************************************************************************/
- package com.cb.eclipse.folding.java.calculation;
- import java.util.HashSet;
- import java.util.Set;
- import org.eclipse.jdt.core.IJavaElement;
- import org.eclipse.jdt.core.JavaModelException;
- import org.eclipse.jdt.core.compiler.ITerminalSymbols;
- import com.cb.eclipse.folding.EnhancedPosition;
- import com.cb.eclipse.folding.EnhancedPosition2;
- import com.cb.eclipse.folding.java.JavaPositionMetadata;
- /**
- * The CommentHelper class is just a common location for the core logic of
- * processing comments and producing comment folding regions.
- *
- * One of the key components of the comment helper class is the comment folding
- * advisor, which is the client provided class the provides the knowledge of
- * whether certain regions should be foldable and/or collapsed initially.
- * Because the comment helper class is generic in nature, the advisor allows for
- * pluggable scenarios.
- *
- * @author R.J. Lorimer
- */
- @SuppressWarnings({"unused", "unchecked"})
- public class CommentHelper {
- private Set regions = new HashSet();
- private int lineCommentCount;
- private int lineCommentStart;
- private int lineCommentEnd;
-
- private CommentFoldingAdvisor advisor;
- private UserDefinedRegionHelper userDefinedRecognizer;
- public CommentHelper() {
- this(DefaultCommentFoldingAdvisor.getInstance());
- }
- public CommentHelper(CommentFoldingAdvisor advisor) {
- this.advisor = advisor;
- userDefinedRecognizer = new UserDefinedRegionHelper();
- }
- public void handle(int token, int start, int end, IJavaElement owner) throws JavaModelException {
- switch(token) {
-
- case ITerminalSymbols.TokenNameCOMMENT_BLOCK:
- handleCommentBlock(start, end);
- break;
- case ITerminalSymbols.TokenNameCOMMENT_JAVADOC:
- handleJavadoc(start, end, owner);
- break;
- default:
- if(ITerminalSymbols.TokenNameCOMMENT_LINE == token && !isUserDefinedSentinel(token, start, end, owner)) {
- handleCommentLine(start, end);
- }
- else {
- handleNonCommentToken(token);
- }
- break;
- }
- }
- public void end() {
- closeOpenComments();
- }
- public Set result() {
- return regions;
- }
- public void reset() {
- regions.clear();
- }
-
- private boolean isUserDefinedSentinel(int token, int start, int end, IJavaElement owner) throws JavaModelException {
- return userDefinedRecognizer.isOpeningSentinel(start, end, owner) || userDefinedRecognizer.isClosingSentinel(start, end, owner);
- }
- private void handleCommentLine(int startPos, int endPos) {
- if (lineCommentCount == 0) {
- lineCommentStart = startPos;
- }
- lineCommentEnd = endPos;
- lineCommentCount++;
- }
- private void handleCommentBlock(int start, int end) {
- if (advisor.shouldFoldBlockComment()) {
- boolean collapse = advisor.shouldCollapseBlockComment();
- regions.add(new EnhancedPosition2(start, end - start, getMetadata(collapse)));
- }
- }
- private void handleJavadoc(int start, int end, IJavaElement owner) {
- if (advisor.shouldFoldJavadoc()) {
- boolean collapse = advisor.shouldCollapseJavadoc();
- regions.add(new EnhancedPosition2(start, end - start, getMetadata(collapse)));
- }
- }
- private void handleNonCommentToken(int token) {
- closeOpenComments();
- }
- private void closeOpenComments() {
- if (lineCommentCount > 1) {
- if (advisor.shouldFoldLineComment()) {
- boolean collapse = advisor.shouldCollapseLineComment();
- regions.add(new EnhancedPosition(lineCommentStart, lineCommentEnd - lineCommentStart - 1, getMetadata(collapse)));
- }
-
- }
- lineCommentCount = 0;
-
- }
- public String toString() {
- return "Comment Helper - regions: " + result().size();
- }
- private JavaPositionMetadata getMetadata(boolean collapse) {
- return new JavaPositionMetadata(false, false, collapse, false, null);
- }
-
- }