blob: 7be609f98e935462db11663fe87b9e425e347d11 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2005, 2021 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.base.ext.ui.text;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.statet.ecommons.preferences.core.PreferenceAccess;
import org.eclipse.statet.ecommons.text.ui.presentation.AbstractRuleBasedScanner;
import org.eclipse.statet.ecommons.text.ui.presentation.ITextPresentationConstants;
import org.eclipse.statet.ecommons.text.ui.settings.TextStyleManager;
import org.eclipse.statet.ecommons.ui.ISettingsChangedHandler;
import org.eclipse.statet.base.core.preferences.TaskTagsPreferences;
/**
* Scanner for comments. Provides support for task tags.
*/
public class CommentScanner extends AbstractRuleBasedScanner implements ISettingsChangedHandler {
private static class TaskTagDetector implements IWordDetector {
@Override
public boolean isWordStart(final char c) {
return Character.isLetterOrDigit(c);
}
@Override
public boolean isWordPart(final char c) {
return Character.isLetterOrDigit(c);
}
}
private static class TaskTagRule extends WordRule {
private final IToken token;
public TaskTagRule(final IToken token, final IToken defaultToken) {
super(new TaskTagDetector(), defaultToken);
this.token= token;
}
public void clearTaskTags() {
this.fWords.clear();
}
public void addTaskTags(final String[] tags) {
for (final String tag : tags) {
addWord(tag, this.token);
}
}
}
private TaskTagRule taskTagRule;
private final String commentTokenKey;
private final String taskTokenKey;
public CommentScanner(final TextStyleManager textStyles, final String commentTokenKey,
final String taskTokenKey,
final PreferenceAccess corePrefs) {
super(textStyles);
this.commentTokenKey= commentTokenKey;
this.taskTokenKey= taskTokenKey;
initRules();
loadTaskTags(corePrefs);
}
@Override
protected void createRules(final List<IRule> rules) {
final IToken defaultToken= getToken(this.commentTokenKey);
final IToken taskToken= getToken(this.taskTokenKey);
setDefaultReturnToken(defaultToken);
// Add rule for Task Tags.
this.taskTagRule= new TaskTagRule(taskToken, defaultToken);
rules.add(this.taskTagRule);
}
@Override
public void handleSettingsChanged(final Set<String> groupIds, final Map<String, Object> options) {
if (groupIds.contains(TaskTagsPreferences.GROUP_ID)) {
final PreferenceAccess prefs= (PreferenceAccess) options.get(ISettingsChangedHandler.PREFERENCEACCESS_KEY);
loadTaskTags(prefs);
options.put(ITextPresentationConstants.SETTINGSCHANGE_AFFECTSPRESENTATION_KEY, Boolean.TRUE);
}
}
public void loadTaskTags(final PreferenceAccess prefs) {
this.taskTagRule.clearTaskTags();
final String[] tags= TaskTagsPreferences.loadTagsOnly(prefs);
if (tags != null) {
this.taskTagRule.addTaskTags(tags);
}
}
}