From b7d86ee8f43a1654050cffbbc536f08adb1fbe94 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Vilain Date: Fri, 3 May 2013 16:39:11 +0200 Subject: [PATCH] SONAR-3893 Reworked symbols API and changed package for corresponding classes --- .../batch/scan/ProjectScanContainer.java | 4 +- .../batch/source/DefaultHighlightable.java | 69 ++++++++ .../org/sonar/batch/source/DefaultSymbol.java | 48 ++++++ .../batch/source/DefaultSymbolTable.java | 102 ++++++++++++ .../batch/source/DefaultSymbolizable.java | 53 +++++++ .../batch/source/HighlightableBuilder.java | 61 +++++++ .../org/sonar/batch/source/SymbolData.java | 66 ++++++++ .../batch/source/SymbolizableBuilder.java | 41 +++++ .../batch/source/SyntaxHighlightingRule.java | 52 ++++++ .../source/SyntaxHighlightingRuleSet.java | 149 ++++++++++++++++++ .../org/sonar/batch/source/package-info.java | 23 +++ .../source/DefaultHighlightableTest.java | 60 +++++++ .../batch/source/DefaultSymbolTableTest.java | 63 ++++++++ .../batch/source/DefaultSymbolizableTest.java | 57 +++++++ .../source/HighlightableBuilderTest.java | 68 ++++++++ .../sonar/batch/source/SymbolDataTest.java | 60 +++++++ .../batch/source/SymbolizableBuilderTest.java | 46 ++++++ .../source/SyntaxHighlightingRuleSetTest.java | 84 ++++++++++ .../sonar/api/scan/source/Highlightable.java | 1 + .../org/sonar/api/scan/source/Symbol.java | 1 + .../api/scan/source/SymbolPerspective.java | 1 + .../org/sonar/api/source/Highlightable.java | 37 +++++ .../java/org/sonar/api/source/Symbol.java | 30 ++++ .../org/sonar/api/source/Symbolizable.java | 50 ++++++ .../org/sonar/api/source/package-info.java | 24 +++ 25 files changed, 1249 insertions(+), 1 deletion(-) create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbol.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/HighlightableBuilder.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/SymbolData.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/SymbolizableBuilder.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/SyntaxHighlightingRule.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/SyntaxHighlightingRuleSet.java create mode 100644 sonar-batch/src/main/java/org/sonar/batch/source/package-info.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/source/DefaultHighlightableTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolizableTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/source/HighlightableBuilderTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/source/SymbolDataTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/source/SymbolizableBuilderTest.java create mode 100644 sonar-batch/src/test/java/org/sonar/batch/source/SyntaxHighlightingRuleSetTest.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/source/Highlightable.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/source/Symbol.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/source/Symbolizable.java create mode 100644 sonar-plugin-api/src/main/java/org/sonar/api/source/package-info.java diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index 2b73b314f58..6173d7d56fa 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -42,6 +42,7 @@ import org.sonar.batch.scan.maven.FakeMavenPluginExecutor; import org.sonar.batch.scan.maven.MavenPluginExecutor; import org.sonar.batch.scan.source.HighlightableBuilder; import org.sonar.batch.scan.source.SymbolPerspectiveBuilder; +import org.sonar.batch.source.SymbolizableBuilder; import org.sonar.core.component.ScanGraph; import org.sonar.core.issue.IssueUpdater; import org.sonar.core.issue.workflow.FunctionExecutor; @@ -110,7 +111,8 @@ public class ProjectScanContainer extends ComponentContainer { // lang HighlightableBuilder.class, - SymbolPerspectiveBuilder.class); + SymbolPerspectiveBuilder.class, + SymbolizableBuilder.class); } private void fixMavenExecutor() { diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java new file mode 100644 index 00000000000..39fb1288317 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultHighlightable.java @@ -0,0 +1,69 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.source; + +import org.sonar.api.component.Component; +import org.sonar.api.source.Highlightable; +import org.sonar.batch.index.ComponentDataCache; +import org.sonar.core.source.SnapshotDataType; + +/** + * @since 3.6 + */ +public class DefaultHighlightable implements Highlightable { + + private final Component component; + private final ComponentDataCache cache; + private final SyntaxHighlightingRuleSet.Builder builder; + + public DefaultHighlightable(Component component, ComponentDataCache cache) { + this.component = component; + this.cache = cache; + this.builder = SyntaxHighlightingRuleSet.builder(); + } + + @Override + public HighlightingBuilder newHighlighting() { + return new DefaultHighlightingBuilder(); + } + + @Override + public Component component() { + return component; + } + + public SyntaxHighlightingRuleSet getHighlightingRules() { + return builder.build(); + } + + private class DefaultHighlightingBuilder implements HighlightingBuilder { + + @Override + public HighlightingBuilder highlight(int startOffset, int endOffset, String typeOfText) { + builder.registerHighlightingRule(startOffset, endOffset, typeOfText); + return this; + } + + @Override + public void done() { + cache.setStringData(component().key(), SnapshotDataType.SYNTAX_HIGHLIGHTING.getValue(), builder.build().writeString()); + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbol.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbol.java new file mode 100644 index 00000000000..99b33ab67d6 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbol.java @@ -0,0 +1,48 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import org.sonar.api.source.Symbol; + +public class DefaultSymbol implements Symbol { + + private final int declarationStartOffset; + private final int declarationEndOffset; + private final String fullyQualifiedName; + + public DefaultSymbol(int startOffset, int endOffset, String fullyQualifiedName) { + this.declarationStartOffset = startOffset; + this.declarationEndOffset = endOffset; + this.fullyQualifiedName = fullyQualifiedName; + } + + public int getDeclarationStartOffset() { + return declarationStartOffset; + } + + public int getDeclarationEndOffset() { + return declarationEndOffset; + } + + public String getFullyQualifiedName() { + return fullyQualifiedName; + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java new file mode 100644 index 00000000000..3813f71ae8e --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolTable.java @@ -0,0 +1,102 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import com.google.common.collect.Multimap; +import com.google.common.collect.TreeMultimap; +import org.sonar.api.source.Symbol; +import org.sonar.api.source.Symbolizable; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class DefaultSymbolTable implements Symbolizable.SymbolTable { + + private Multimap referencesBySymbol; + + private DefaultSymbolTable(Multimap referencesBySymbol) { + this.referencesBySymbol = referencesBySymbol; + } + + public Multimap getReferencesBySymbol() { + return referencesBySymbol; + } + + @Override + public List symbols() { + return new ArrayList(referencesBySymbol.keySet()); + } + + @Override + public List references(Symbol symbol) { + return new ArrayList(referencesBySymbol.get(symbol)); + } + + public static class Builder implements Symbolizable.SymbolTableBuilder { + + private final Multimap referencesBySymbol; + + public Builder() { + referencesBySymbol = TreeMultimap.create(new SymbolComparator(), new ReferenceComparator()); + } + + @Override + public Symbol newSymbol(int fromOffset, int toOffset) { + Symbol symbol = new DefaultSymbol(fromOffset, toOffset, null); + referencesBySymbol.put(symbol, symbol.getDeclarationStartOffset()); + return symbol; + } + + @Override + public void newReference(Symbol symbol, int fromOffset) { + if (fromOffset >= symbol.getDeclarationStartOffset() && fromOffset < symbol.getDeclarationEndOffset()) { + throw new UnsupportedOperationException("Cannot add reference overlapping the symbol declaration"); + } + referencesBySymbol.put(symbol, fromOffset); + } + + @Override + public Symbolizable.SymbolTable build() { + return new DefaultSymbolTable(referencesBySymbol); + } + + private class SymbolComparator implements Comparator { + @Override + public int compare(Symbol left, Symbol right) { + return left.getDeclarationStartOffset() - right.getDeclarationStartOffset(); + } + } + + private class ReferenceComparator implements Comparator { + @Override + public int compare(Integer left, Integer right) { + int result; + if (left != null & right != null) { + result = left - right; + } else { + result = left == null ? -1 : 1; + } + return result; + } + } + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java new file mode 100644 index 00000000000..ca30c040d75 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/DefaultSymbolizable.java @@ -0,0 +1,53 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import org.sonar.api.component.Component; +import org.sonar.api.source.Symbolizable; +import org.sonar.batch.index.ComponentDataCache; +import org.sonar.core.source.SnapshotDataType; + +public class DefaultSymbolizable implements Symbolizable { + + private final ComponentDataCache cache; + private final Component component; + + public DefaultSymbolizable(ComponentDataCache cache, Component component) { + this.cache = cache; + this.component = component; + } + + @Override + public Component component() { + return component; + } + + @Override + public SymbolTableBuilder newSymbolTableBuilder() { + return new DefaultSymbolTable.Builder(); + } + + @Override + public void setSymbolTable(SymbolTable symbolTable) { + SymbolData symbolData = new SymbolData(symbolTable); + cache.setStringData(component().key(), SnapshotDataType.SYMBOL_HIGHLIGHTING.getValue(), symbolData.writeString()); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/HighlightableBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/source/HighlightableBuilder.java new file mode 100644 index 00000000000..34e510525df --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/HighlightableBuilder.java @@ -0,0 +1,61 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.source; + +import com.google.common.collect.ImmutableSet; +import org.sonar.api.component.Component; +import org.sonar.api.resources.Qualifiers; +import org.sonar.api.resources.Scopes; +import org.sonar.api.source.Highlightable; +import org.sonar.batch.index.ComponentDataCache; +import org.sonar.core.component.PerspectiveBuilder; +import org.sonar.core.component.ResourceComponent; + +import javax.annotation.CheckForNull; +import java.util.Set; + +/** + * @since 3.6 + */ +public class HighlightableBuilder extends PerspectiveBuilder { + + private static final Set SUPPORTED_QUALIFIERS = ImmutableSet.of(Qualifiers.FILE, Qualifiers.CLASS, Qualifiers.UNIT_TEST_FILE); + private final ComponentDataCache cache; + + public HighlightableBuilder(ComponentDataCache cache) { + super(Highlightable.class); + this.cache = cache; + } + + @CheckForNull + @Override + protected Highlightable loadPerspective(Class perspectiveClass, Component component) { + boolean supported = SUPPORTED_QUALIFIERS.contains(component.qualifier()); + if (supported && component instanceof ResourceComponent) { + // temporary hack waiting for the removal of JavaClass. + // JavaClass has the same qualifier than JavaFile, so they have to distinguished by their scope + supported = Scopes.FILE.equals(((ResourceComponent) component).scope()); + } + if (supported) { + return new DefaultHighlightable(component, cache); + } + return null; + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/SymbolData.java b/sonar-batch/src/main/java/org/sonar/batch/source/SymbolData.java new file mode 100644 index 00000000000..6513e70b92e --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/SymbolData.java @@ -0,0 +1,66 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import com.google.common.collect.Multimap; +import org.sonar.api.source.Symbol; +import org.sonar.api.source.Symbolizable; +import org.sonar.batch.index.Data; + +import java.util.Collection; + +public class SymbolData implements Data { + + private static final String FIELD_SEPARATOR = ","; + private static final String SYMBOL_SEPARATOR = ";"; + + private final Symbolizable.SymbolTable symbolTable; + + public SymbolData(Symbolizable.SymbolTable symbolTable) { + this.symbolTable = symbolTable; + } + + @Override + public String writeString() { + StringBuilder sb = new StringBuilder(); + + Multimap referencesBySymbol = ((DefaultSymbolTable)symbolTable).getReferencesBySymbol(); + + for (Symbol symbol : ((DefaultSymbolTable)symbolTable).getReferencesBySymbol().keySet()) { + + sb.append(symbol.getDeclarationStartOffset()) + .append(FIELD_SEPARATOR) + .append(symbol.getDeclarationEndOffset()); + Collection symbolReferences = referencesBySymbol.get(symbol); + for (Integer symbolReference : symbolReferences) { + sb.append(FIELD_SEPARATOR).append(symbolReference); + } + sb.append(SYMBOL_SEPARATOR); + } + + return sb.toString(); + } + + @Override + public void readString(String s) { + throw new UnsupportedOperationException(); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/SymbolizableBuilder.java b/sonar-batch/src/main/java/org/sonar/batch/source/SymbolizableBuilder.java new file mode 100644 index 00000000000..a341bb2d32d --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/SymbolizableBuilder.java @@ -0,0 +1,41 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import org.sonar.api.component.Component; +import org.sonar.api.source.Symbolizable; +import org.sonar.batch.index.ComponentDataCache; +import org.sonar.core.component.PerspectiveBuilder; + +public class SymbolizableBuilder extends PerspectiveBuilder { + + private final ComponentDataCache cache; + + public SymbolizableBuilder(ComponentDataCache cache) { + super(Symbolizable.class); + this.cache = cache; + } + + @Override + protected Symbolizable loadPerspective(Class perspectiveClass, Component component) { + return new DefaultSymbolizable(cache, component); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/SyntaxHighlightingRule.java b/sonar-batch/src/main/java/org/sonar/batch/source/SyntaxHighlightingRule.java new file mode 100644 index 00000000000..9ebae160a2b --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/SyntaxHighlightingRule.java @@ -0,0 +1,52 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.source; + +/** + * @since 3.6 + */ +public class SyntaxHighlightingRule { + + private final int startPosition; + private final int endPosition; + private final String textType; + + private SyntaxHighlightingRule(int startPosition, int endPosition, String textType) { + this.startPosition = startPosition; + this.endPosition = endPosition; + this.textType = textType; + } + + public static SyntaxHighlightingRule create(int startPosition, int endPosition, String textType) { + return new SyntaxHighlightingRule(startPosition, endPosition, textType); + } + + public int getStartPosition() { + return startPosition; + } + + public int getEndPosition() { + return endPosition; + } + + public String getTextType() { + return textType; + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/SyntaxHighlightingRuleSet.java b/sonar-batch/src/main/java/org/sonar/batch/source/SyntaxHighlightingRuleSet.java new file mode 100644 index 00000000000..f367f3bf1db --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/SyntaxHighlightingRuleSet.java @@ -0,0 +1,149 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.source; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Predicate; +import com.google.common.collect.Collections2; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Ordering; +import org.slf4j.LoggerFactory; +import org.sonar.batch.index.Data; + +import javax.annotation.Nullable; +import java.util.Collection; +import java.util.List; + +import static com.google.common.collect.Lists.newArrayList; + +/** + * @since 3.6 + */ +public class SyntaxHighlightingRuleSet implements Data { + + private static final String FIELD_SEPARATOR = ","; + private static final String RULE_SEPARATOR = ";"; + + private List syntaxHighlightingRuleSet; + + private SyntaxHighlightingRuleSet(List syntaxHighlightingRules) { + syntaxHighlightingRuleSet = syntaxHighlightingRules; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private List syntaxHighlightingRuleSet; + + public Builder() { + syntaxHighlightingRuleSet = newArrayList(); + } + + public Builder registerHighlightingRule(int startOffset, int endOffset, String typeOfText) { + if (ruleConflictsWithExistingRules(startOffset, endOffset)) { + String errorMsg = String.format("Cannot register highlighting rule for characters from %s to %s as it " + + "overlaps at least one existing rule", startOffset, endOffset); + LoggerFactory.getLogger(SyntaxHighlightingRuleSet.class).error(errorMsg); + throw new UnsupportedOperationException(errorMsg); + } + SyntaxHighlightingRule syntaxHighlightingRule = SyntaxHighlightingRule.create(startOffset, endOffset, + typeOfText); + this.syntaxHighlightingRuleSet.add(syntaxHighlightingRule); + return this; + } + + public SyntaxHighlightingRuleSet build() { + return new SyntaxHighlightingRuleSet(ImmutableList.copyOf(syntaxHighlightingRuleSet)); + } + + private boolean ruleConflictsWithExistingRules(final int startOffset, final int endOffset) { + Collection conflictingRules = Collections2 + .filter(syntaxHighlightingRuleSet, new Predicate() { + @Override + public boolean apply(@Nullable SyntaxHighlightingRule syntaxHighlightingRule) { + + if (syntaxHighlightingRule != null) { + boolean overlapsStartBoundary = startOffset < syntaxHighlightingRule.getStartPosition() + && endOffset >= syntaxHighlightingRule.getStartPosition() + 1 + && endOffset < syntaxHighlightingRule.getEndPosition(); + + boolean overlapsEndBoundary = startOffset > syntaxHighlightingRule.getStartPosition() + && startOffset < syntaxHighlightingRule.getEndPosition() + && endOffset > syntaxHighlightingRule.getEndPosition(); + + return overlapsStartBoundary || overlapsEndBoundary; + } + return false; + } + }); + return !conflictingRules.isEmpty(); + } + } + + public List getSyntaxHighlightingRuleSet() { + return syntaxHighlightingRuleSet; + } + + @Override + public String writeString() { + StringBuilder sb = new StringBuilder(); + List orderedRules = getOrderedHighlightingRules(); + + for (SyntaxHighlightingRule highlightingRule : orderedRules) { + sb.append(highlightingRule.getStartPosition()) + .append(FIELD_SEPARATOR) + .append(highlightingRule.getEndPosition()) + .append(FIELD_SEPARATOR) + .append(highlightingRule.getTextType()) + .append(RULE_SEPARATOR); + } + + return sb.toString(); + } + + @Override + public void readString(String s) { + throw new UnsupportedOperationException(); + } + + @VisibleForTesting + protected List getOrderedHighlightingRules() { + Ordering ruleOrdering = new Ordering() { + @Override + public int compare(@Nullable SyntaxHighlightingRule left, + @Nullable SyntaxHighlightingRule right) { + int result = 0; + if (left != null && right != null) { + result = left.getStartPosition() - right.getStartPosition(); + if (result == 0) { + result = left.getEndPosition() - right.getEndPosition(); + } + return result; + } + return left != null ? 1 : -1; + } + }; + + return ruleOrdering.sortedCopy(syntaxHighlightingRuleSet); + } +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/source/package-info.java b/sonar-batch/src/main/java/org/sonar/batch/source/package-info.java new file mode 100644 index 00000000000..1e9d90e825c --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/source/package-info.java @@ -0,0 +1,23 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +@ParametersAreNonnullByDefault +package org.sonar.batch.source; + +import javax.annotation.ParametersAreNonnullByDefault; diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/DefaultHighlightableTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultHighlightableTest.java new file mode 100644 index 00000000000..b08289de62f --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultHighlightableTest.java @@ -0,0 +1,60 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.source; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.component.Component; +import org.sonar.batch.index.ComponentDataCache; +import org.sonar.core.source.SnapshotDataType; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.*; + +public class DefaultHighlightableTest { + + @Rule + public ExpectedException throwable = ExpectedException.none(); + + @Test + public void should_store_highlighting_rules() throws Exception { + DefaultHighlightable highlightablePerspective = new DefaultHighlightable(null, null); + highlightablePerspective.newHighlighting().highlight(0, 10, "k").highlight(20, 30, "cppd"); + + assertThat(highlightablePerspective.getHighlightingRules().getSyntaxHighlightingRuleSet()).hasSize(2); + } + + @Test + public void should_apply_registered_highlighting() throws Exception { + Component component = mock(Component.class); + when(component.key()).thenReturn("myComponent"); + + ComponentDataCache cache = mock(ComponentDataCache.class); + + DefaultHighlightable highlightable = new DefaultHighlightable(component, cache); + highlightable.newHighlighting() + .highlight(0, 10, "k") + .highlight(20, 30, "cppd") + .done(); + + verify(cache).setStringData("myComponent", SnapshotDataType.SYNTAX_HIGHLIGHTING.getValue(), "0,10,k;20,30,cppd;"); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java new file mode 100644 index 00000000000..57f2960989e --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolTableTest.java @@ -0,0 +1,63 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.source.Symbol; +import org.sonar.api.source.Symbolizable; + +import static org.fest.assertions.Assertions.assertThat; + +public class DefaultSymbolTableTest { + + @Rule + public ExpectedException throwable = ExpectedException.none(); + + @Test + public void should_order_symbol_and_references() throws Exception { + + Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); + Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20); + symbolTableBuilder.newReference(firstSymbol, 32); + Symbol secondSymbol = symbolTableBuilder.newSymbol(84, 92); + symbolTableBuilder.newReference(secondSymbol, 124); + Symbol thirdSymbol = symbolTableBuilder.newSymbol(55, 62); + symbolTableBuilder.newReference(thirdSymbol, 70); + Symbolizable.SymbolTable symbolTable = symbolTableBuilder.build(); + + assertThat(symbolTable.symbols()).containsExactly(firstSymbol, thirdSymbol, secondSymbol); + assertThat(symbolTable.references(firstSymbol)).containsExactly(10, 32); + assertThat(symbolTable.references(secondSymbol)).containsExactly(84, 124); + assertThat(symbolTable.references(thirdSymbol)).containsExactly(55, 70); + } + + @Test + public void should_reject_reference_conflicting_with_declaration() throws Exception { + + throwable.expect(UnsupportedOperationException.class); + + Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); + Symbol symbol = symbolTableBuilder.newSymbol(10, 20); + symbolTableBuilder.newReference(symbol, 15); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolizableTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolizableTest.java new file mode 100644 index 00000000000..197fdbc3a7d --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/source/DefaultSymbolizableTest.java @@ -0,0 +1,57 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import org.junit.Test; +import org.sonar.api.component.Component; +import org.sonar.api.source.Symbol; +import org.sonar.api.source.Symbolizable; +import org.sonar.batch.index.ComponentDataCache; +import org.sonar.core.source.SnapshotDataType; + +import static org.mockito.Mockito.*; + +public class DefaultSymbolizableTest { + + @Test + public void should_update_cache_when_done() throws Exception { + + Component component = mock(Component.class); + when(component.key()).thenReturn("myComponent"); + + ComponentDataCache cache = mock(ComponentDataCache.class); + + DefaultSymbolizable symbolPerspective = new DefaultSymbolizable(cache, component); + Symbolizable.SymbolTableBuilder symbolTableBuilder = symbolPerspective.newSymbolTableBuilder(); + Symbol firstSymbol = symbolTableBuilder.newSymbol(4, 8); + symbolTableBuilder.newReference(firstSymbol, 12); + symbolTableBuilder.newReference(firstSymbol, 70); + Symbol otherSymbol = symbolTableBuilder.newSymbol(25, 33); + symbolTableBuilder.newReference(otherSymbol, 44); + symbolTableBuilder.newReference(otherSymbol, 60); + symbolTableBuilder.newReference(otherSymbol, 108); + Symbolizable.SymbolTable symbolTable = symbolTableBuilder.build(); + + symbolPerspective.setSymbolTable(symbolTable); + + verify(cache).setStringData("myComponent", SnapshotDataType.SYMBOL_HIGHLIGHTING.getValue(), "4,8,4,12,70;25,33,25,44,60,108;"); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/HighlightableBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/HighlightableBuilderTest.java new file mode 100644 index 00000000000..e535d30f5af --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/source/HighlightableBuilderTest.java @@ -0,0 +1,68 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.source; + +import org.junit.Test; +import org.sonar.api.component.Component; +import org.sonar.api.resources.File; +import org.sonar.api.resources.Project; +import org.sonar.api.source.Highlightable; +import org.sonar.batch.index.ComponentDataCache; +import org.sonar.core.component.ResourceComponent; +import org.sonar.java.api.JavaClass; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class HighlightableBuilderTest { + + ComponentDataCache cache = mock(ComponentDataCache.class); + + @Test + public void should_load_default_perspective() throws Exception { + Component component = new ResourceComponent(new File("foo/bar.c")); + + HighlightableBuilder builder = new HighlightableBuilder(cache); + Highlightable perspective = builder.loadPerspective(Highlightable.class, component); + + assertThat(perspective).isNotNull().isInstanceOf(DefaultHighlightable.class); + assertThat(perspective.component()).isSameAs(component); + } + + @Test + public void project_should_not_be_highlightable() { + Component component = new ResourceComponent(new Project("Foo")); + + HighlightableBuilder builder = new HighlightableBuilder(cache); + Highlightable perspective = builder.loadPerspective(Highlightable.class, component); + + assertThat(perspective).isNull(); + } + + @Test + public void java_class_should_not_be_highlightable() { + Component component = new ResourceComponent(JavaClass.create("foo", "Bar")); + + HighlightableBuilder builder = new HighlightableBuilder(cache); + Highlightable perspective = builder.loadPerspective(Highlightable.class, component); + + assertThat(perspective).isNull(); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/SymbolDataTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/SymbolDataTest.java new file mode 100644 index 00000000000..6644a76d0c2 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/source/SymbolDataTest.java @@ -0,0 +1,60 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import org.junit.Test; +import org.sonar.api.source.Symbol; +import org.sonar.api.source.Symbolizable; + +import static org.fest.assertions.Assertions.assertThat; + +public class SymbolDataTest { + + @Test + public void should_serialize_symbols_in_natural_order() throws Exception { + + Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); + Symbol firstSymbol = symbolTableBuilder.newSymbol(10, 20); + symbolTableBuilder.newReference(firstSymbol, 32); + Symbol secondSymbol = symbolTableBuilder.newSymbol(84, 92); + symbolTableBuilder.newReference(secondSymbol, 124); + Symbol thirdSymbol = symbolTableBuilder.newSymbol(55, 62); + symbolTableBuilder.newReference(thirdSymbol, 70); + Symbolizable.SymbolTable symbolTable = symbolTableBuilder.build(); + + SymbolData dataRepository = new SymbolData(symbolTable); + String serializedSymbolData = dataRepository.writeString(); + + assertThat(serializedSymbolData).isEqualTo("10,20,10,32;55,62,55,70;84,92,84,124;"); + } + + @Test + public void should_serialize_unused_symbol() throws Exception { + + Symbolizable.SymbolTableBuilder symbolTableBuilder = new DefaultSymbolTable.Builder(); + symbolTableBuilder.newSymbol(10, 20); + + SymbolData dataRepository = new SymbolData(symbolTableBuilder.build()); + String serializedSymbolData = dataRepository.writeString(); + + assertThat(serializedSymbolData).isEqualTo("10,20,10;"); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/SymbolizableBuilderTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/SymbolizableBuilderTest.java new file mode 100644 index 00000000000..3ab82b7c397 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/source/SymbolizableBuilderTest.java @@ -0,0 +1,46 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.batch.source; + +import org.junit.Test; +import org.sonar.api.component.Component; +import org.sonar.api.component.Perspective; +import org.sonar.api.source.Symbolizable; +import org.sonar.batch.index.ComponentDataCache; + +import static org.fest.assertions.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +public class SymbolizableBuilderTest { + + ComponentDataCache dataCache = mock(ComponentDataCache.class); + + @Test + public void should_load_perspective() throws Exception { + Component component = mock(Component.class); + + SymbolizableBuilder perspectiveBuilder = new SymbolizableBuilder(dataCache); + Perspective perspective = perspectiveBuilder.loadPerspective(Symbolizable.class, component); + + assertThat(perspective).isInstanceOf(Symbolizable.class); + assertThat(perspective.component()).isEqualTo(component); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/source/SyntaxHighlightingRuleSetTest.java b/sonar-batch/src/test/java/org/sonar/batch/source/SyntaxHighlightingRuleSetTest.java new file mode 100644 index 00000000000..0db6dc5116f --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/source/SyntaxHighlightingRuleSetTest.java @@ -0,0 +1,84 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.batch.source; + + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import java.util.List; + +import static org.fest.assertions.Assertions.assertThat; + +public class SyntaxHighlightingRuleSetTest { + + private SyntaxHighlightingRuleSet highlightingRules; + + @Rule + public ExpectedException throwable = ExpectedException.none(); + + @Before + public void setUpSampleRules() { + + SyntaxHighlightingRuleSet.Builder highlightingRuleSet = SyntaxHighlightingRuleSet.builder(); + highlightingRuleSet.registerHighlightingRule(0, 10, "cd"); + highlightingRuleSet.registerHighlightingRule(10, 12, "k"); + highlightingRuleSet.registerHighlightingRule(24, 38, "k"); + highlightingRuleSet.registerHighlightingRule(42, 50, "k"); + highlightingRuleSet.registerHighlightingRule(24, 65, "cppd"); + highlightingRuleSet.registerHighlightingRule(12, 20, "cd"); + + highlightingRules = highlightingRuleSet.build(); + } + + @Test + public void should_register_highlighting_rule() throws Exception { + assertThat(highlightingRules.getSyntaxHighlightingRuleSet()).hasSize(6); + } + + @Test + public void should_order_by_start_then_end_offset() throws Exception { + + List orderedRules = highlightingRules.getOrderedHighlightingRules(); + + assertThat(orderedRules).onProperty("startPosition").containsExactly(0, 10, 12, 24, 24, 42); + assertThat(orderedRules).onProperty("endPosition").containsExactly(10, 12, 20, 38, 65, 50); + assertThat(orderedRules).onProperty("textType").containsExactly("cd", "k", "cd", "k", "cppd", "k"); + } + + @Test + public void should_serialize_rules_to_string() throws Exception { + + String serializedRules = highlightingRules.writeString(); + assertThat(serializedRules).isEqualTo("0,10,cd;10,12,k;12,20,cd;24,38,k;24,65,cppd;42,50,k;"); + } + + @Test + public void should_prevent_rules_overlapping() throws Exception { + + throwable.expect(UnsupportedOperationException.class); + + SyntaxHighlightingRuleSet.Builder builder = SyntaxHighlightingRuleSet.builder(); + builder.registerHighlightingRule(0, 10, "k"); + builder.registerHighlightingRule(8, 15, "k"); + } +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/Highlightable.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/Highlightable.java index 359f0b27d4c..78bcbc6256f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/Highlightable.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/Highlightable.java @@ -24,6 +24,7 @@ import org.sonar.api.component.Perspective; /** * @since 3.6 */ +@Deprecated public interface Highlightable extends Perspective { interface HighlightingBuilder { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/Symbol.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/Symbol.java index ac2996d68d7..df9d1e8d6cc 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/Symbol.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/Symbol.java @@ -20,6 +20,7 @@ package org.sonar.api.scan.source; +@Deprecated public interface Symbol { public int getDeclarationStartOffset(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/SymbolPerspective.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/SymbolPerspective.java index f09ea4ddd4b..0a82f58a476 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/SymbolPerspective.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/source/SymbolPerspective.java @@ -25,6 +25,7 @@ import org.sonar.api.component.Perspective; /** * @since 3.6 */ +@Deprecated public interface SymbolPerspective extends Perspective { interface SymbolBuilder { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/source/Highlightable.java b/sonar-plugin-api/src/main/java/org/sonar/api/source/Highlightable.java new file mode 100644 index 00000000000..05cb7e9f72a --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/source/Highlightable.java @@ -0,0 +1,37 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.source; + +import org.sonar.api.component.Perspective; + +/** + * @since 3.6 + */ +public interface Highlightable extends Perspective { + + interface HighlightingBuilder { + + HighlightingBuilder highlight(int startOffset, int endOffset, String typeOfText); + + void done(); + } + + HighlightingBuilder newHighlighting(); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/source/Symbol.java b/sonar-plugin-api/src/main/java/org/sonar/api/source/Symbol.java new file mode 100644 index 00000000000..2b7df78db97 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/source/Symbol.java @@ -0,0 +1,30 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +package org.sonar.api.source; + +public interface Symbol { + + public int getDeclarationStartOffset(); + + public int getDeclarationEndOffset(); + + public String getFullyQualifiedName(); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/source/Symbolizable.java b/sonar-plugin-api/src/main/java/org/sonar/api/source/Symbolizable.java new file mode 100644 index 00000000000..94c5370f347 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/source/Symbolizable.java @@ -0,0 +1,50 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.source; + +import org.sonar.api.component.Perspective; + +import java.util.List; + +/** + * @since 3.6 + */ +public interface Symbolizable extends Perspective { + + interface SymbolTableBuilder { + + Symbol newSymbol(int fromOffset, int toOffset); + + void newReference(Symbol symbol, int fromOffset); + + SymbolTable build(); + } + + interface SymbolTable { + + List symbols(); + + List references(Symbol symbol); + } + + SymbolTableBuilder newSymbolTableBuilder(); + + void setSymbolTable(SymbolTable symbolTable); +} diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/source/package-info.java b/sonar-plugin-api/src/main/java/org/sonar/api/source/package-info.java new file mode 100644 index 00000000000..fa29a0e231c --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/source/package-info.java @@ -0,0 +1,24 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +@ParametersAreNonnullByDefault +package org.sonar.api.source; + +import javax.annotation.ParametersAreNonnullByDefault; \ No newline at end of file -- 2.39.5