Merge pull request #1269 from eclipse/kt/issue1569/3

[eclipse/xtext#1569] Refactor Xtend to Java
This commit is contained in:
Karsten Thoms 2019-11-04 12:34:24 +01:00 committed by GitHub
commit 5e70dd0360
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 568 additions and 1087 deletions

View file

@ -0,0 +1,102 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import static com.google.common.collect.Iterables.*;
import static java.util.Collections.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.documentation.impl.AbstractMultiLineCommentProvider;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parsetree.reconstr.IHiddenTokenHelper;
import org.eclipse.xtext.resource.XtextResource;
import com.google.inject.Inject;
import com.google.inject.name.Named;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
public class DefaultTaskFinder implements ITaskFinder {
@Inject
private ITaskParser parser;
@Inject
private ITaskTagProvider taskTagProvider;
@Inject
private IHiddenTokenHelper hiddenTokenHelper;
private Pattern endTagPattern = Pattern.compile("\\*/\\z");
/**
* this method is not intended to be called by clients
*
* @since 2.12
*/
@Inject(optional = true)
protected Pattern setEndTag(@Named(AbstractMultiLineCommentProvider.END_TAG) String endTag) {
return endTagPattern = Pattern.compile(endTag + "\\z");
}
@Override
public List<Task> findTasks(Resource resource) {
TaskTags taskTags = taskTagProvider.getTaskTags(resource);
if (isEmpty(taskTags) || !(resource instanceof XtextResource)) {
return emptyList();
}
IParseResult parseResult = ((XtextResource) resource).getParseResult();
if (parseResult == null || parseResult.getRootNode() == null) {
return emptyList();
}
List<Task> tasks = findTasks(parseResult.getRootNode(), taskTags);
return unmodifiableList(tasks);
}
protected List<Task> findTasks(ICompositeNode node, TaskTags taskTags) {
ArrayList<Task> result = new ArrayList<>();
node.getLeafNodes().forEach(leaf -> result.addAll(this.findTasks(leaf, taskTags)));
return result;
}
protected List<Task> findTasks(ILeafNode node, TaskTags taskTags) {
if (!canContainTaskTags(node)) {
return emptyList();
}
List<Task> tasks = parser.parseTasks(stripText(node, node.getText()), taskTags);
tasks.forEach(it -> {
it.setOffset(it.getOffset() + node.getOffset());
it.setLineNumber(it.getLineNumber() + node.getStartLine() - 1);
});
return tasks;
}
/**
* @since 2.12
*/
protected String stripText(ILeafNode node, String text) {
return endTagPattern.matcher(text).replaceAll("");
}
protected boolean canContainTaskTags(ILeafNode node) {
EObject rule = node.getGrammarElement();
if (rule instanceof AbstractRule) {
return hiddenTokenHelper.isComment((AbstractRule) rule);
}
return false;
}
}

View file

@ -1,88 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.tasks
import com.google.inject.Inject
import com.google.inject.name.Named
import java.util.Collections
import java.util.List
import java.util.regex.Pattern
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.AbstractRule
import org.eclipse.xtext.documentation.impl.AbstractMultiLineCommentProvider
import org.eclipse.xtext.nodemodel.ICompositeNode
import org.eclipse.xtext.nodemodel.ILeafNode
import org.eclipse.xtext.parsetree.reconstr.IHiddenTokenHelper
import org.eclipse.xtext.resource.XtextResource
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
class DefaultTaskFinder implements ITaskFinder {
@Inject
ITaskParser parser
@Inject
ITaskTagProvider taskTagProvider
@Inject
IHiddenTokenHelper hiddenTokenHelper
Pattern endTagPattern = Pattern.compile("\\*/\\z")
/**
* this method is not intended to be called by clients
* @since 2.12
*/
@Inject(optional=true)
protected def setEndTag(@Named(AbstractMultiLineCommentProvider.END_TAG) String endTag) {
endTagPattern = Pattern.compile(endTag+"\\z")
}
override findTasks(Resource resource) {
val taskTags = taskTagProvider.getTaskTags(resource)
if(taskTags.empty) return #[]
if (resource instanceof XtextResource) {
resource.parseResult?.rootNode?.findTasks(taskTags) ?: #[]
} else {
#[]
}
}
protected def List<Task> findTasks(ICompositeNode it, TaskTags taskTags) {
val result = newArrayList
leafNodes.forEach[result.addAll(findTasks(taskTags))]
return result
}
protected def List<Task> findTasks(ILeafNode node, TaskTags taskTags) {
if (node.canContainTaskTags) {
//TODO strip comment characters before parsing, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=380449#c13
val tasks = parser.parseTasks(stripText(node, node.text), taskTags)
tasks.forEach [
offset = offset + node.offset
lineNumber = lineNumber + node.startLine - 1
]
return tasks
}
return Collections.emptyList
}
/**
* @since 2.12
*/
protected def String stripText(ILeafNode node, String text) {
return endTagPattern.matcher(text).replaceAll("")
}
protected def boolean canContainTaskTags(ILeafNode node) {
val rule = node.grammarElement
if (rule instanceof AbstractRule) {
return hiddenTokenHelper.isComment(rule)
}
return false;
}
}

View file

@ -0,0 +1,64 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import static com.google.common.collect.Iterables.*;
import static java.util.Collections.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.xtext.util.Strings;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
public class DefaultTaskParser implements ITaskParser {
@Override
public List<Task> parseTasks(String source, TaskTags taskTags) {
if (isEmpty(taskTags)) {
return emptyList();
}
Map<String, TaskTag> taskTagsByName = getTaskTagsByName(taskTags);
Matcher matcher = toPattern(taskTags).matcher(source);
List<Task> tasks = null;
int prevLine = 1;
int prevOffset = 0;
while (matcher.find()) {
if (tasks == null) {
tasks = new ArrayList<>();
}
Task task = new Task();
String matchedTag = matcher.group(2);
TaskTag tag = taskTagsByName.get(taskTags.isCaseSensitive() ? matchedTag : matchedTag.toLowerCase());
task.setTag(tag);
task.setDescription(matcher.group(3));
task.setOffset(matcher.start(2));
int countLineBreaks = Strings.countLineBreaks(source, prevOffset, task.getOffset());
task.setLineNumber(countLineBreaks + prevLine);
prevLine = task.getLineNumber();
prevOffset = task.getOffset();
tasks.add(task);
}
return tasks != null ? tasks : emptyList();
}
protected Map<String, TaskTag> getTaskTagsByName(TaskTags taskTags) {
return taskTags.getTaskTagsByName();
}
protected Pattern toPattern(TaskTags taskTags) {
return taskTags.toPattern();
}
}

View file

@ -1,54 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.tasks
import java.util.Collections
import java.util.List
import java.util.Map
import org.eclipse.xtext.util.Strings
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
class DefaultTaskParser implements ITaskParser {
override parseTasks(String source, TaskTags taskTags) {
if (taskTags.empty) return Collections.emptyList
val taskTagsByName = getTaskTagsByName(taskTags)
val matcher = toPattern(taskTags).matcher(source)
var List<Task> tasks
// keep track of the offset and line numbers to avoid unnecessary line counting from start
var prevLine = 1;
var prevOffset = 0;
while (matcher.find) {
if (tasks === null)
tasks = newArrayList
val task = new Task()
val matchedTag = matcher.group(2)
task.tag = taskTagsByName.get(if (taskTags.caseSensitive) matchedTag else matchedTag.toLowerCase)
task.description = matcher.group(3)
task.offset = matcher.start(2)
task.lineNumber = Strings.countLineBreaks(source, prevOffset, task.offset) + prevLine
prevLine = task.lineNumber
prevOffset = task.offset
tasks += task
}
tasks ?: Collections.emptyList
}
protected def Map<String, TaskTag> getTaskTagsByName(TaskTags taskTags) {
return taskTags.taskTagsByName
}
protected def toPattern(TaskTags taskTags) {
return taskTags.toPattern
}
}

View file

@ -0,0 +1,33 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import org.eclipse.emf.ecore.resource.Resource;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
public class DefaultTaskTagProvider implements ITaskTagProvider {
@Override
public TaskTags getTaskTags(Resource resource) {
TaskTags taskTags = new TaskTags();
taskTags.setCaseSensitive(true);
taskTags.getTaskTags().add(toTaskTag("TODO", Priority.NORMAL));
taskTags.getTaskTags().add(toTaskTag("FIXME", Priority.HIGH));
taskTags.getTaskTags().add(toTaskTag("XXX", Priority.NORMAL));
return taskTags;
}
private TaskTag toTaskTag(String name, Priority priority) {
TaskTag tag = new TaskTag();
tag.setName(name);
tag.setPriority(priority);
return tag;
}
}

View file

@ -1,38 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.tasks
import org.eclipse.emf.ecore.resource.Resource
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
class DefaultTaskTagProvider implements ITaskTagProvider {
override getTaskTags(Resource resource) {
new TaskTags => [
caseSensitive = true
taskTags += #[
new TaskTag => [
name = "TODO"
priority = Priority.NORMAL
],
new TaskTag => [
name = "FIXME"
priority = Priority.HIGH
],
new TaskTag => [
name = "XXX"
priority = Priority.NORMAL
]
]
]
}
}

View file

@ -0,0 +1,87 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import static java.util.stream.Collectors.*;
import static org.eclipse.xtext.xbase.lib.IterableExtensions.toList;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.preferences.IPreferenceValues;
import org.eclipse.xtext.preferences.IPreferenceValuesProvider;
import org.eclipse.xtext.preferences.PreferenceKey;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import com.google.common.base.Splitter;
import com.google.inject.Inject;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
public class PreferenceTaskTagProvider implements ITaskTagProvider {
public static final PreferenceKey TAGS_KEY = new PreferenceKey("task.tags", "TODO,FIXME,XXX");
public static final PreferenceKey PRIORITIES_KEY = new PreferenceKey("task.priorities", "NORMAL,HIGH,NORMAL");
public static final PreferenceKey CASE_SENSITIVE_KEY = new PreferenceKey("task.caseSensitive", "true");
public static final List<PreferenceKey> KEYS = Collections.<PreferenceKey>unmodifiableList(
CollectionLiterals.<PreferenceKey>newArrayList(PreferenceTaskTagProvider.TAGS_KEY,
PreferenceTaskTagProvider.PRIORITIES_KEY, PreferenceTaskTagProvider.CASE_SENSITIVE_KEY));
public static List<TaskTag> parseTags(String names, String priorities) {
Splitter splitter = Splitter.on(",").omitEmptyStrings().trimResults();
List<String> tags = toList(splitter.split(names));
List<String> prios = toList(splitter.split(priorities));
List<TaskTag> elements = new ArrayList<>();
for (int i=0; i<tags.size(); i++) {
TaskTag taskTag = new TaskTag();
taskTag.setName(tags.get(i));
Priority priority = Priority.NORMAL;
if (prios.size() >= i) {
try {
priority = Priority.valueOf(prios.get(i));
} catch (IllegalArgumentException ignore) {}
}
taskTag.setPriority(priority);
elements.add(taskTag);
}
return elements;
}
public static String serializeTags(List<TaskTag> tags) {
return tags.stream().map(t -> t.getName()).collect(joining(","));
}
public static String serializePriorities(List<TaskTag> tags) {
return tags.stream().map(t -> t.getPriority().toString()).collect(joining(","));
}
private IPreferenceValuesProvider preferenceValuesProvider;
@Override
public TaskTags getTaskTags(Resource resource) {
IPreferenceValues prefs = this.preferenceValuesProvider.getPreferenceValues(resource);
String names = prefs.getPreference(PreferenceTaskTagProvider.TAGS_KEY);
String priorities = prefs.getPreference(PreferenceTaskTagProvider.PRIORITIES_KEY);
TaskTags taskTags = new TaskTags();
taskTags.setCaseSensitive(Boolean.valueOf(prefs.getPreference(PreferenceTaskTagProvider.CASE_SENSITIVE_KEY)));
List<TaskTag> tags = PreferenceTaskTagProvider.parseTags(names, priorities);
taskTags.getTaskTags().addAll(tags);
return taskTags;
}
@Inject
public void setPreferenceValuesProvider(IPreferenceValuesProvider preferenceValuesProvider) {
this.preferenceValuesProvider = preferenceValuesProvider;
}
}

View file

@ -1,76 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.tasks
import com.google.common.base.Joiner
import com.google.common.base.Splitter
import com.google.inject.Inject
import java.util.List
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.preferences.IPreferenceValuesProvider
import org.eclipse.xtext.preferences.PreferenceKey
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
class PreferenceTaskTagProvider implements ITaskTagProvider {
public static val TAGS_KEY = new PreferenceKey("task.tags", "TODO,FIXME,XXX")
public static val PRIORITIES_KEY = new PreferenceKey("task.priorities", "NORMAL,HIGH,NORMAL")
public static val CASE_SENSITIVE_KEY = new PreferenceKey("task.caseSensitive", "true")
public static val KEYS = #[TAGS_KEY, PRIORITIES_KEY, CASE_SENSITIVE_KEY]
static def List<TaskTag> parseTags(String names, String priorities) {
val splitter = Splitter.on(',').omitEmptyStrings.trimResults
val tags = splitter.split(names).toList
val prios = splitter.split(priorities).toList
val elements = newArrayList
for (i : 0 ..< tags.size) {
elements += new TaskTag => [
name = tags.get(i)
priority =
if (prios.size >= i) {
try {
Priority.valueOf(prios.get(i))
} catch (IllegalArgumentException e) {
Priority.NORMAL
}
} else {
Priority.NORMAL
}
]
}
return elements
}
static def String serializeTags(List<TaskTag> tags) {
Joiner.on(',').join(tags.map[name])
}
static def String serializePriorities(List<TaskTag> tags) {
Joiner.on(',').join(tags.map[priority])
}
IPreferenceValuesProvider preferenceValuesProvider
override getTaskTags(Resource resource) {
val prefs = preferenceValuesProvider.getPreferenceValues(resource)
val names = prefs.getPreference(TAGS_KEY)
val priorities = prefs.getPreference(PRIORITIES_KEY)
new TaskTags => [
caseSensitive = Boolean.valueOf(prefs.getPreference(CASE_SENSITIVE_KEY))
getTaskTags += parseTags(names, priorities)
]
}
@Inject
def void setPreferenceValuesProvider(IPreferenceValuesProvider preferenceValuesProvider) {
this.preferenceValuesProvider = preferenceValuesProvider
}
}

View file

@ -0,0 +1,123 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
public class Task {
private TaskTag tag;
private String description;
private int lineNumber;
private int offset;
public String getFullText() {
return tag.getName() + description;
}
public int getTotalLength() {
return getFullText().length();
}
public int getTagLength() {
return tag.length();
}
public TaskTag getTag() {
return tag;
}
public void setTag(TaskTag tag) {
this.tag = tag;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public int getLineNumber() {
return lineNumber;
}
public void setLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Task other = (Task) obj;
if (tag == null) {
if (other.tag != null) {
return false;
}
} else if (!tag.equals(other.tag)) {
return false;
}
if (description == null) {
if (other.description != null) {
return false;
}
} else if (!description.equals(other.description)) {
return false;
}
if (other.lineNumber != lineNumber) {
return false;
}
if (other.offset != offset) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (tag == null ? 0 : tag.hashCode());
result = prime * result + (description == null ? 0 : description.hashCode());
result = prime * result + lineNumber;
return prime * result + offset;
}
@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("tag", getTag());
b.add("description", getDescription());
b.add("lineNumber", getLineNumber());
b.add("offset", getOffset());
return super.toString();
}
}

View file

@ -1,36 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.tasks
import org.eclipse.xtend.lib.annotations.EqualsHashCode
import org.eclipse.xtend.lib.annotations.Accessors
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@Accessors
@EqualsHashCode
class Task {
TaskTag tag;
String description;
int lineNumber;
int offset;
def String getFullText() {
tag.name + description
}
def int getTotalLength() {
fullText.length
}
def int getTagLength() {
tag.length
}
}

View file

@ -0,0 +1,75 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
public class TaskTag {
private String name;
private Priority priority;
public int length() {
return name.length();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Priority getPriority() {
return priority;
}
public void setPriority(Priority priority) {
this.priority = priority;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
TaskTag other = (TaskTag) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (priority == null) {
if (other.priority != null) {
return false;
}
} else if (!priority.equals(other.priority)) {
return false;
}
return true;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (name == null ? 0 : name.hashCode());
return prime * result + (priority == null ? 0 : priority.hashCode());
}
}

View file

@ -1,84 +0,0 @@
/*******************************************************************************
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*******************************************************************************/
package org.eclipse.xtext.tasks
import java.util.HashMap
import java.util.List
import java.util.Map
import java.util.regex.Pattern
import org.eclipse.xtend.lib.annotations.Accessors
import org.eclipse.xtend.lib.annotations.EqualsHashCode
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@Accessors
@EqualsHashCode
class TaskTag {
String name
Priority priority
def length() {
name.length
}
}
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@Accessors
class TaskTags implements Iterable<TaskTag> {
boolean caseSensitive
val List<TaskTag> taskTags = newArrayList
@Accessors(NONE)
var Pattern pattern
@Accessors(NONE)
var Map<String, TaskTag> taskTagsByName
override iterator() {
taskTags.iterator
}
def Pattern toPattern() {
if (pattern === null) {
// for projects with a lot of comments around 5-10% of full build is spend to compile pattern
// hence we cache it
var flags = Pattern.MULTILINE
if (!caseSensitive) {
flags = flags.bitwiseOr(Pattern.CASE_INSENSITIVE).bitwiseOr(Pattern.UNICODE_CASE)
}
pattern = Pattern.compile('''^.*((«taskTags.map[Pattern.quote(name)].join("|")»)(.*)?)$''', flags)
}
return pattern
}
def Map<String, TaskTag> getTaskTagsByName() {
if (taskTagsByName === null) {
taskTagsByName = new HashMap<String, TaskTag>
for (tag : taskTags) {
val name = if (caseSensitive) tag.name else tag.name.toLowerCase
val oldTag = taskTagsByName.get(name)
if (oldTag !== null) {
// prioritize higher priority tags
if (tag.priority.ordinal < oldTag.priority.ordinal) {
taskTagsByName.put(name, tag)
}
} else {
taskTagsByName.put(name, tag)
}
}
}
return taskTagsByName
}
}

View file

@ -0,0 +1,84 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import static java.util.stream.Collectors.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
public class TaskTags implements Iterable<TaskTag> {
private boolean caseSensitive;
private final List<TaskTag> taskTags = new ArrayList<>();
private Pattern pattern;
private Map<String, TaskTag> taskTagsByName;
@Override
public Iterator<TaskTag> iterator() {
return taskTags.iterator();
}
public Pattern toPattern() {
if (pattern == null) {
int flags = Pattern.MULTILINE;
if (!caseSensitive) {
flags = flags | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
}
StringBuilder builder = new StringBuilder();
builder.append("^.*((");
String tagNames = taskTags.stream().map(it -> Pattern.quote(it.getName())).collect(joining("|"));
builder.append(tagNames);
builder.append(")(.*)?)$");
pattern = Pattern.compile(builder.toString(), flags);
}
return pattern;
}
public Map<String, TaskTag> getTaskTagsByName() {
if (taskTagsByName == null) {
HashMap<String, TaskTag> name2tag = new HashMap<>();
taskTagsByName = name2tag;
for (TaskTag tag : taskTags) {
String name = caseSensitive ? tag.getName() : tag.getName().toLowerCase();
TaskTag oldTag = taskTagsByName.get(name);
if (oldTag != null) {
// prioritize higher priority tags
if (tag.getPriority().ordinal() < oldTag.getPriority().ordinal()) {
taskTagsByName.put(name, tag);
}
} else {
taskTagsByName.put(name, tag);
}
}
}
return taskTagsByName;
}
public boolean isCaseSensitive() {
return caseSensitive;
}
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
public List<TaskTag> getTaskTags() {
return taskTags;
}
}

View file

@ -1,139 +0,0 @@
/**
* Copyright (c) 2014, 2017 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import com.google.inject.Inject;
import com.google.inject.name.Named;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.documentation.impl.AbstractMultiLineCommentProvider;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.parser.IParseResult;
import org.eclipse.xtext.parsetree.reconstr.IHiddenTokenHelper;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.tasks.ITaskFinder;
import org.eclipse.xtext.tasks.ITaskParser;
import org.eclipse.xtext.tasks.ITaskTagProvider;
import org.eclipse.xtext.tasks.Task;
import org.eclipse.xtext.tasks.TaskTags;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@SuppressWarnings("all")
public class DefaultTaskFinder implements ITaskFinder {
@Inject
private ITaskParser parser;
@Inject
private ITaskTagProvider taskTagProvider;
@Inject
private IHiddenTokenHelper hiddenTokenHelper;
private Pattern endTagPattern = Pattern.compile("\\*/\\z");
/**
* this method is not intended to be called by clients
* @since 2.12
*/
@Inject(optional = true)
protected Pattern setEndTag(@Named(AbstractMultiLineCommentProvider.END_TAG) final String endTag) {
return this.endTagPattern = Pattern.compile((endTag + "\\z"));
}
@Override
public List<Task> findTasks(final Resource resource) {
List<Task> _xblockexpression = null;
{
final TaskTags taskTags = this.taskTagProvider.getTaskTags(resource);
boolean _isEmpty = IterableExtensions.isEmpty(taskTags);
if (_isEmpty) {
return Collections.<Task>unmodifiableList(CollectionLiterals.<Task>newArrayList());
}
List<Task> _xifexpression = null;
if ((resource instanceof XtextResource)) {
List<Task> _elvis = null;
IParseResult _parseResult = ((XtextResource)resource).getParseResult();
ICompositeNode _rootNode = null;
if (_parseResult!=null) {
_rootNode=_parseResult.getRootNode();
}
List<Task> _findTasks = null;
if (_rootNode!=null) {
_findTasks=this.findTasks(_rootNode, taskTags);
}
if (_findTasks != null) {
_elvis = _findTasks;
} else {
_elvis = Collections.<Task>unmodifiableList(CollectionLiterals.<Task>newArrayList());
}
_xifexpression = _elvis;
} else {
_xifexpression = Collections.<Task>unmodifiableList(CollectionLiterals.<Task>newArrayList());
}
_xblockexpression = _xifexpression;
}
return _xblockexpression;
}
protected List<Task> findTasks(final ICompositeNode it, final TaskTags taskTags) {
final ArrayList<Task> result = CollectionLiterals.<Task>newArrayList();
final Consumer<ILeafNode> _function = (ILeafNode it_1) -> {
result.addAll(this.findTasks(it_1, taskTags));
};
it.getLeafNodes().forEach(_function);
return result;
}
protected List<Task> findTasks(final ILeafNode node, final TaskTags taskTags) {
boolean _canContainTaskTags = this.canContainTaskTags(node);
if (_canContainTaskTags) {
final List<Task> tasks = this.parser.parseTasks(this.stripText(node, node.getText()), taskTags);
final Consumer<Task> _function = (Task it) -> {
int _offset = it.getOffset();
int _offset_1 = node.getOffset();
int _plus = (_offset + _offset_1);
it.setOffset(_plus);
int _lineNumber = it.getLineNumber();
int _startLine = node.getStartLine();
int _plus_1 = (_lineNumber + _startLine);
int _minus = (_plus_1 - 1);
it.setLineNumber(_minus);
};
tasks.forEach(_function);
return tasks;
}
return Collections.<Task>emptyList();
}
/**
* @since 2.12
*/
protected String stripText(final ILeafNode node, final String text) {
return this.endTagPattern.matcher(text).replaceAll("");
}
protected boolean canContainTaskTags(final ILeafNode node) {
final EObject rule = node.getGrammarElement();
if ((rule instanceof AbstractRule)) {
return this.hiddenTokenHelper.isComment(((AbstractRule)rule));
}
return false;
}
}

View file

@ -1,86 +0,0 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.xtext.tasks.ITaskParser;
import org.eclipse.xtext.tasks.Task;
import org.eclipse.xtext.tasks.TaskTag;
import org.eclipse.xtext.tasks.TaskTags;
import org.eclipse.xtext.util.Strings;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@SuppressWarnings("all")
public class DefaultTaskParser implements ITaskParser {
@Override
public List<Task> parseTasks(final String source, final TaskTags taskTags) {
List<Task> _xblockexpression = null;
{
boolean _isEmpty = IterableExtensions.isEmpty(taskTags);
if (_isEmpty) {
return Collections.<Task>emptyList();
}
final Map<String, TaskTag> taskTagsByName = this.getTaskTagsByName(taskTags);
final Matcher matcher = this.toPattern(taskTags).matcher(source);
List<Task> tasks = null;
int prevLine = 1;
int prevOffset = 0;
while (matcher.find()) {
{
if ((tasks == null)) {
tasks = CollectionLiterals.<Task>newArrayList();
}
final Task task = new Task();
final String matchedTag = matcher.group(2);
String _xifexpression = null;
boolean _isCaseSensitive = taskTags.isCaseSensitive();
if (_isCaseSensitive) {
_xifexpression = matchedTag;
} else {
_xifexpression = matchedTag.toLowerCase();
}
task.setTag(taskTagsByName.get(_xifexpression));
task.setDescription(matcher.group(3));
task.setOffset(matcher.start(2));
int _countLineBreaks = Strings.countLineBreaks(source, prevOffset, task.getOffset());
int _plus = (_countLineBreaks + prevLine);
task.setLineNumber(_plus);
prevLine = task.getLineNumber();
prevOffset = task.getOffset();
tasks.add(task);
}
}
List<Task> _elvis = null;
if (tasks != null) {
_elvis = tasks;
} else {
List<Task> _emptyList = Collections.<Task>emptyList();
_elvis = _emptyList;
}
_xblockexpression = _elvis;
}
return _xblockexpression;
}
protected Map<String, TaskTag> getTaskTagsByName(final TaskTags taskTags) {
return taskTags.getTaskTagsByName();
}
protected Pattern toPattern(final TaskTags taskTags) {
return taskTags.toPattern();
}
}

View file

@ -1,56 +0,0 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import com.google.common.collect.Iterables;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.tasks.ITaskTagProvider;
import org.eclipse.xtext.tasks.Priority;
import org.eclipse.xtext.tasks.TaskTag;
import org.eclipse.xtext.tasks.TaskTags;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@SuppressWarnings("all")
public class DefaultTaskTagProvider implements ITaskTagProvider {
@Override
public TaskTags getTaskTags(final Resource resource) {
TaskTags _taskTags = new TaskTags();
final Procedure1<TaskTags> _function = (TaskTags it) -> {
it.setCaseSensitive(true);
List<TaskTag> _taskTags_1 = it.getTaskTags();
TaskTag _taskTag = new TaskTag();
final Procedure1<TaskTag> _function_1 = (TaskTag it_1) -> {
it_1.setName("TODO");
it_1.setPriority(Priority.NORMAL);
};
TaskTag _doubleArrow = ObjectExtensions.<TaskTag>operator_doubleArrow(_taskTag, _function_1);
TaskTag _taskTag_1 = new TaskTag();
final Procedure1<TaskTag> _function_2 = (TaskTag it_1) -> {
it_1.setName("FIXME");
it_1.setPriority(Priority.HIGH);
};
TaskTag _doubleArrow_1 = ObjectExtensions.<TaskTag>operator_doubleArrow(_taskTag_1, _function_2);
TaskTag _taskTag_2 = new TaskTag();
final Procedure1<TaskTag> _function_3 = (TaskTag it_1) -> {
it_1.setName("XXX");
it_1.setPriority(Priority.NORMAL);
};
TaskTag _doubleArrow_2 = ObjectExtensions.<TaskTag>operator_doubleArrow(_taskTag_2, _function_3);
Iterables.<TaskTag>addAll(_taskTags_1, Collections.<TaskTag>unmodifiableList(CollectionLiterals.<TaskTag>newArrayList(_doubleArrow, _doubleArrow_1, _doubleArrow_2)));
};
return ObjectExtensions.<TaskTags>operator_doubleArrow(_taskTags, _function);
}
}

View file

@ -1,124 +0,0 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.preferences.IPreferenceValues;
import org.eclipse.xtext.preferences.IPreferenceValuesProvider;
import org.eclipse.xtext.preferences.PreferenceKey;
import org.eclipse.xtext.tasks.ITaskTagProvider;
import org.eclipse.xtext.tasks.Priority;
import org.eclipse.xtext.tasks.TaskTag;
import org.eclipse.xtext.tasks.TaskTags;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.ExclusiveRange;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@SuppressWarnings("all")
public class PreferenceTaskTagProvider implements ITaskTagProvider {
public static final PreferenceKey TAGS_KEY = new PreferenceKey("task.tags", "TODO,FIXME,XXX");
public static final PreferenceKey PRIORITIES_KEY = new PreferenceKey("task.priorities", "NORMAL,HIGH,NORMAL");
public static final PreferenceKey CASE_SENSITIVE_KEY = new PreferenceKey("task.caseSensitive", "true");
public static final List<PreferenceKey> KEYS = Collections.<PreferenceKey>unmodifiableList(CollectionLiterals.<PreferenceKey>newArrayList(PreferenceTaskTagProvider.TAGS_KEY, PreferenceTaskTagProvider.PRIORITIES_KEY, PreferenceTaskTagProvider.CASE_SENSITIVE_KEY));
public static List<TaskTag> parseTags(final String names, final String priorities) {
final Splitter splitter = Splitter.on(",").omitEmptyStrings().trimResults();
final List<String> tags = IterableExtensions.<String>toList(splitter.split(names));
final List<String> prios = IterableExtensions.<String>toList(splitter.split(priorities));
final ArrayList<TaskTag> elements = CollectionLiterals.<TaskTag>newArrayList();
int _size = tags.size();
ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, _size, true);
for (final Integer i : _doubleDotLessThan) {
TaskTag _taskTag = new TaskTag();
final Procedure1<TaskTag> _function = (TaskTag it) -> {
it.setName(tags.get((i).intValue()));
Priority _xifexpression = null;
int _size_1 = prios.size();
boolean _greaterEqualsThan = (_size_1 >= (i).intValue());
if (_greaterEqualsThan) {
Priority _xtrycatchfinallyexpression = null;
try {
_xtrycatchfinallyexpression = Priority.valueOf(prios.get((i).intValue()));
} catch (final Throwable _t) {
if (_t instanceof IllegalArgumentException) {
_xtrycatchfinallyexpression = Priority.NORMAL;
} else {
throw Exceptions.sneakyThrow(_t);
}
}
_xifexpression = _xtrycatchfinallyexpression;
} else {
_xifexpression = Priority.NORMAL;
}
it.setPriority(_xifexpression);
};
TaskTag _doubleArrow = ObjectExtensions.<TaskTag>operator_doubleArrow(_taskTag, _function);
elements.add(_doubleArrow);
}
return elements;
}
public static String serializeTags(final List<TaskTag> tags) {
final Function1<TaskTag, String> _function = (TaskTag it) -> {
return it.getName();
};
return Joiner.on(",").join(ListExtensions.<TaskTag, String>map(tags, _function));
}
public static String serializePriorities(final List<TaskTag> tags) {
final Function1<TaskTag, Priority> _function = (TaskTag it) -> {
return it.getPriority();
};
return Joiner.on(",").join(ListExtensions.<TaskTag, Priority>map(tags, _function));
}
private IPreferenceValuesProvider preferenceValuesProvider;
@Override
public TaskTags getTaskTags(final Resource resource) {
TaskTags _xblockexpression = null;
{
final IPreferenceValues prefs = this.preferenceValuesProvider.getPreferenceValues(resource);
final String names = prefs.getPreference(PreferenceTaskTagProvider.TAGS_KEY);
final String priorities = prefs.getPreference(PreferenceTaskTagProvider.PRIORITIES_KEY);
TaskTags _taskTags = new TaskTags();
final Procedure1<TaskTags> _function = (TaskTags it) -> {
it.setCaseSensitive((Boolean.valueOf(prefs.getPreference(PreferenceTaskTagProvider.CASE_SENSITIVE_KEY))).booleanValue());
List<TaskTag> _taskTags_1 = it.getTaskTags();
List<TaskTag> _parseTags = PreferenceTaskTagProvider.parseTags(names, priorities);
Iterables.<TaskTag>addAll(_taskTags_1, _parseTags);
};
_xblockexpression = ObjectExtensions.<TaskTags>operator_doubleArrow(_taskTags, _function);
}
return _xblockexpression;
}
@Inject
public void setPreferenceValuesProvider(final IPreferenceValuesProvider preferenceValuesProvider) {
this.preferenceValuesProvider = preferenceValuesProvider;
}
}

View file

@ -1,117 +0,0 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtend.lib.annotations.EqualsHashCode;
import org.eclipse.xtext.tasks.TaskTag;
import org.eclipse.xtext.xbase.lib.Pure;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@Accessors
@EqualsHashCode
@SuppressWarnings("all")
public class Task {
private TaskTag tag;
private String description;
private int lineNumber;
private int offset;
public String getFullText() {
String _name = this.tag.getName();
return (_name + this.description);
}
public int getTotalLength() {
return this.getFullText().length();
}
public int getTagLength() {
return this.tag.length();
}
@Pure
public TaskTag getTag() {
return this.tag;
}
public void setTag(final TaskTag tag) {
this.tag = tag;
}
@Pure
public String getDescription() {
return this.description;
}
public void setDescription(final String description) {
this.description = description;
}
@Pure
public int getLineNumber() {
return this.lineNumber;
}
public void setLineNumber(final int lineNumber) {
this.lineNumber = lineNumber;
}
@Pure
public int getOffset() {
return this.offset;
}
public void setOffset(final int offset) {
this.offset = offset;
}
@Override
@Pure
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Task other = (Task) obj;
if (this.tag == null) {
if (other.tag != null)
return false;
} else if (!this.tag.equals(other.tag))
return false;
if (this.description == null) {
if (other.description != null)
return false;
} else if (!this.description.equals(other.description))
return false;
if (other.lineNumber != this.lineNumber)
return false;
if (other.offset != this.offset)
return false;
return true;
}
@Override
@Pure
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.tag== null) ? 0 : this.tag.hashCode());
result = prime * result + ((this.description== null) ? 0 : this.description.hashCode());
result = prime * result + this.lineNumber;
return prime * result + this.offset;
}
}

View file

@ -1,80 +0,0 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtend.lib.annotations.EqualsHashCode;
import org.eclipse.xtext.tasks.Priority;
import org.eclipse.xtext.xbase.lib.Pure;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@Accessors
@EqualsHashCode
@SuppressWarnings("all")
public class TaskTag {
private String name;
private Priority priority;
public int length() {
return this.name.length();
}
@Pure
public String getName() {
return this.name;
}
public void setName(final String name) {
this.name = name;
}
@Pure
public Priority getPriority() {
return this.priority;
}
public void setPriority(final Priority priority) {
this.priority = priority;
}
@Override
@Pure
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
TaskTag other = (TaskTag) obj;
if (this.name == null) {
if (other.name != null)
return false;
} else if (!this.name.equals(other.name))
return false;
if (this.priority == null) {
if (other.priority != null)
return false;
} else if (!this.priority.equals(other.priority))
return false;
return true;
}
@Override
@Pure
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.name== null) ? 0 : this.name.hashCode());
return prime * result + ((this.priority== null) ? 0 : this.priority.hashCode());
}
}

View file

@ -1,109 +0,0 @@
/**
* Copyright (c) 2014 itemis AG (http://www.itemis.eu) and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.eclipse.xtext.tasks;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.eclipse.xtend.lib.annotations.AccessorType;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.tasks.TaskTag;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.Pure;
/**
* @author Stefan Oehme - Initial contribution and API
* @since 2.6
*/
@Accessors
@SuppressWarnings("all")
public class TaskTags implements Iterable<TaskTag> {
private boolean caseSensitive;
private final List<TaskTag> taskTags = CollectionLiterals.<TaskTag>newArrayList();
@Accessors(AccessorType.NONE)
private Pattern pattern;
@Accessors(AccessorType.NONE)
private Map<String, TaskTag> taskTagsByName;
@Override
public Iterator<TaskTag> iterator() {
return this.taskTags.iterator();
}
public Pattern toPattern() {
if ((this.pattern == null)) {
int flags = Pattern.MULTILINE;
if ((!this.caseSensitive)) {
flags = ((flags | Pattern.CASE_INSENSITIVE) | Pattern.UNICODE_CASE);
}
StringConcatenation _builder = new StringConcatenation();
_builder.append("^.*((");
final Function1<TaskTag, String> _function = (TaskTag it) -> {
return Pattern.quote(it.getName());
};
String _join = IterableExtensions.join(ListExtensions.<TaskTag, String>map(this.taskTags, _function), "|");
_builder.append(_join);
_builder.append(")(.*)?)$");
this.pattern = Pattern.compile(_builder.toString(), flags);
}
return this.pattern;
}
public Map<String, TaskTag> getTaskTagsByName() {
if ((this.taskTagsByName == null)) {
HashMap<String, TaskTag> _hashMap = new HashMap<String, TaskTag>();
this.taskTagsByName = _hashMap;
for (final TaskTag tag : this.taskTags) {
{
String _xifexpression = null;
if (this.caseSensitive) {
_xifexpression = tag.getName();
} else {
_xifexpression = tag.getName().toLowerCase();
}
final String name = _xifexpression;
final TaskTag oldTag = this.taskTagsByName.get(name);
if ((oldTag != null)) {
int _ordinal = tag.getPriority().ordinal();
int _ordinal_1 = oldTag.getPriority().ordinal();
boolean _lessThan = (_ordinal < _ordinal_1);
if (_lessThan) {
this.taskTagsByName.put(name, tag);
}
} else {
this.taskTagsByName.put(name, tag);
}
}
}
}
return this.taskTagsByName;
}
@Pure
public boolean isCaseSensitive() {
return this.caseSensitive;
}
public void setCaseSensitive(final boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
@Pure
public List<TaskTag> getTaskTags() {
return this.taskTags;
}
}