private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) { Preconditions.checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0), "Start pointer %s should be before end pointer %s", start, end); return new DefaultTextRange(start, end); }
/** * Return list of symbol references ranges for the symbol at a given position in a file. * * @param componentKey Key of the file like 'myProjectKey:src/foo.php' * @param line Line you want to query * @param lineOffset Offset you want to query. * @return List of references for the symbol (potentially empty) or null if there is no symbol at this position. */ @CheckForNull public Collection<TextRange> referencesForSymbolAt(String componentKey, int line, int lineOffset) { DefaultSymbolTable symbolTable = sensorStorage.symbolsPerComponent.get(componentKey); if (symbolTable == null) { return null; } DefaultTextPointer location = new DefaultTextPointer(line, lineOffset); for (Map.Entry<TextRange, Set<TextRange>> symbol : symbolTable.getReferencesBySymbol().entrySet()) { if (symbol.getKey().start().compareTo(location) <= 0 && symbol.getKey().end().compareTo(location) > 0) { return symbol.getValue(); } } return null; }
@Override public NewSymbol newSymbol(TextRange range) { checkInputFileNotNull(); TreeSet<TextRange> references = new TreeSet<>((o1, o2) -> o1.start().compareTo(o2.start())); referencesBySymbol.put(range, references); return new DefaultSymbol(inputFile, range, references); }
private void checkOverlappingBoundaries() { if (syntaxHighlightingRules.size() > 1) { Iterator<SyntaxHighlightingRule> it = syntaxHighlightingRules.iterator(); SyntaxHighlightingRule previous = it.next(); while (it.hasNext()) { SyntaxHighlightingRule current = it.next(); if (previous.range().end().compareTo(current.range().start()) > 0 && (previous.range().end().compareTo(current.range().end()) < 0)) { String errorMsg = String.format("Cannot register highlighting rule for characters at %s as it " + "overlaps at least one existing rule", current.range()); throw new IllegalStateException(errorMsg); } previous = current; } } }
/** * Return list of syntax highlighting applied for a given position in a file. The result is a list because in theory you * can apply several styles to the same range. * * @param componentKey Key of the file like 'myProjectKey:src/foo.php' * @param line Line you want to query * @param lineOffset Offset you want to query. * @return List of styles applied to this position or empty list if there is no highlighting at this position. */ public List<TypeOfText> highlightingTypeAt(String componentKey, int line, int lineOffset) { DefaultHighlighting syntaxHighlightingData = sensorStorage.highlightingByComponent.get(componentKey); if (syntaxHighlightingData == null) { return Collections.emptyList(); } List<TypeOfText> result = new ArrayList<>(); DefaultTextPointer location = new DefaultTextPointer(line, lineOffset); for (SyntaxHighlightingRule sortedRule : syntaxHighlightingData.getSyntaxHighlightingRuleSet()) { if (sortedRule.range().start().compareTo(location) <= 0 && sortedRule.range().end().compareTo(location) > 0) { result.add(sortedRule.getTextType()); } } return result; }
@Override public DefaultCpdTokens addToken(TextRange range, String image) { requireNonNull(range, "Range should not be null"); requireNonNull(image, "Image should not be null"); checkInputFileNotNull(); if (isExcludedForDuplication()) { return this; } checkState(lastRange == null || lastRange.end().compareTo(range.start()) <= 0, "Tokens of file %s should be provided in order.\nPrevious token: %s\nLast token: %s", inputFile, lastRange, range); String value = image; int line = range.start().line(); if (line != startLine) { addNewTokensLine(result, startIndex, currentIndex, startLine, sb); startIndex = currentIndex + 1; startLine = line; } currentIndex++; sb.append(value); lastRange = range; return this; }
@Override protected void doSave() { checkInputFileNotNull(); // Sort rules to avoid variation during consecutive runs Collections.sort(syntaxHighlightingRules, (left, right) -> { int result = left.range().start().compareTo(right.range().start()); if (result == 0) { result = right.range().end().compareTo(left.range().end()); } return result; }); checkOverlappingBoundaries(); storage.store(this); }
/** * Get highlighting types at a given position in an inputfile * * @param lineOffset 0-based offset in file */ public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) { int ref = ((DefaultInputComponent) file).scannerId(); if (!reader.hasSyntaxHighlighting(ref)) { return Collections.emptyList(); } TextPointer pointer = file.newPointer(line, lineOffset); List<TypeOfText> result = new ArrayList<>(); try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) { while (it.hasNext()) { ScannerReport.SyntaxHighlightingRule rule = it.next(); TextRange ruleRange = toRange(file, rule.getRange()); if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { result.add(ScannerReportUtils.toBatchType(rule.getType())); } } } catch (Exception e) { throw new IllegalStateException("Can't read syntax highlighting for " + file, e); } return result; }
private static TextRange newRangeValidPointers(TextPointer start, TextPointer end, boolean acceptEmptyRange) { Preconditions.checkArgument(acceptEmptyRange ? (start.compareTo(end) <= 0) : (start.compareTo(end) < 0), "Start pointer %s should be before end pointer %s", start, end); return new DefaultTextRange(start, end); }
@Override public int compare(TextRange o1, TextRange o2) { return o1.start().compareTo(o2.start()); } }));
/** * Return list of symbol references ranges for the symbol at a given position in a file. * * @param componentKey Key of the file like 'myProjectKey:src/foo.php' * @param line Line you want to query * @param lineOffset Offset you want to query. * @return List of references for the symbol (potentially empty) or null if there is no symbol at this position. */ @CheckForNull public Collection<TextRange> referencesForSymbolAt(String componentKey, int line, int lineOffset) { DefaultSymbolTable symbolTable = sensorStorage.symbolsPerComponent.get(componentKey); if (symbolTable == null) { return null; } DefaultTextPointer location = new DefaultTextPointer(line, lineOffset); for (Map.Entry<TextRange, Set<TextRange>> symbol : symbolTable.getReferencesBySymbol().entrySet()) { if (symbol.getKey().start().compareTo(location) <= 0 && symbol.getKey().end().compareTo(location) > 0) { return symbol.getValue(); } } return null; }
@Override public NewSymbol newSymbol(TextRange range) { checkInputFileNotNull(); TreeSet<TextRange> references = new TreeSet<>((o1, o2) -> o1.start().compareTo(o2.start())); referencesBySymbol.put(range, references); return new DefaultSymbol(inputFile, range, references); }
private void checkOverlappingBoundaries() { if (syntaxHighlightingRules.size() > 1) { Iterator<SyntaxHighlightingRule> it = syntaxHighlightingRules.iterator(); SyntaxHighlightingRule previous = it.next(); while (it.hasNext()) { SyntaxHighlightingRule current = it.next(); if (previous.range().end().compareTo(current.range().start()) > 0 && (previous.range().end().compareTo(current.range().end()) < 0)) { String errorMsg = String.format("Cannot register highlighting rule for characters at %s as it " + "overlaps at least one existing rule", current.range()); throw new IllegalStateException(errorMsg); } previous = current; } } }
/** * Return list of syntax highlighting applied for a given position in a file. The result is a list because in theory you * can apply several styles to the same range. * * @param componentKey Key of the file like 'myProjectKey:src/foo.php' * @param line Line you want to query * @param lineOffset Offset you want to query. * @return List of styles applied to this position or empty list if there is no highlighting at this position. */ public List<TypeOfText> highlightingTypeAt(String componentKey, int line, int lineOffset) { DefaultHighlighting syntaxHighlightingData = sensorStorage.highlightingByComponent.get(componentKey); if (syntaxHighlightingData == null) { return Collections.emptyList(); } List<TypeOfText> result = new ArrayList<>(); DefaultTextPointer location = new DefaultTextPointer(line, lineOffset); for (SyntaxHighlightingRule sortedRule : syntaxHighlightingData.getSyntaxHighlightingRuleSet()) { if (sortedRule.range().start().compareTo(location) <= 0 && sortedRule.range().end().compareTo(location) > 0) { result.add(sortedRule.getTextType()); } } return result; }
/** * Get highlighting types at a given position in an inputfile * * @param lineOffset 0-based offset in file */ public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) { int ref = ((DefaultInputComponent) file).scannerId(); if (!reader.hasSyntaxHighlighting(ref)) { return Collections.emptyList(); } TextPointer pointer = file.newPointer(line, lineOffset); List<TypeOfText> result = new ArrayList<>(); try (CloseableIterator<ScannerReport.SyntaxHighlightingRule> it = reader.readComponentSyntaxHighlighting(ref)) { while (it.hasNext()) { ScannerReport.SyntaxHighlightingRule rule = it.next(); TextRange ruleRange = toRange(file, rule.getRange()); if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { result.add(ScannerReportUtils.toBatchType(rule.getType())); } } } catch (Exception e) { throw new IllegalStateException("Can't read syntax highlighting for " + file, e); } return result; }
@Override protected void doSave() { checkInputFileNotNull(); // Sort rules to avoid variation during consecutive runs Collections.sort(syntaxHighlightingRules, (left, right) -> { int result = left.range().start().compareTo(right.range().start()); if (result == 0) { result = right.range().end().compareTo(left.range().end()); } return result; }); checkOverlappingBoundaries(); storage.store(this); }
@Override public DefaultCpdTokens addToken(TextRange range, String image) { requireNonNull(range, "Range should not be null"); requireNonNull(image, "Image should not be null"); checkInputFileNotNull(); if (isExcludedForDuplication()) { return this; } checkState(lastRange == null || lastRange.end().compareTo(range.start()) <= 0, "Tokens of file %s should be provided in order.\nPrevious token: %s\nLast token: %s", inputFile, lastRange, range); String value = image; int line = range.start().line(); if (line != startLine) { addNewTokensLine(result, startIndex, currentIndex, startLine, sb); startIndex = currentIndex + 1; startLine = line; } currentIndex++; sb.append(value); lastRange = range; return this; }
/** * Get highlighting types at a given position in an inputfile * @param lineOffset 0-based offset in file */ public List<TypeOfText> highlightingTypeFor(InputFile file, int line, int lineOffset) { int ref = reportComponents.get(((DefaultInputFile) file).key()).getRef(); if (!reader.hasSyntaxHighlighting(ref)) { return Collections.emptyList(); } TextPointer pointer = file.newPointer(line, lineOffset); List<TypeOfText> result = new ArrayList<>(); try (CloseableIterator<BatchReport.SyntaxHighlighting> it = reader.readComponentSyntaxHighlighting(ref)) { while (it.hasNext()) { BatchReport.SyntaxHighlighting rule = it.next(); TextRange ruleRange = toRange(file, rule.getRange()); if (ruleRange.start().compareTo(pointer) <= 0 && ruleRange.end().compareTo(pointer) > 0) { result.add(BatchReportUtils.toBatchType(rule.getType())); } } } catch (Exception e) { throw new IllegalStateException("Can't read syntax highlighting for " + file.absolutePath(), e); } return result; }