From 4c38f3cc064971556a8966c83b879b192cb27748 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Vilain Date: Wed, 10 Apr 2013 18:09:33 +0200 Subject: [PATCH] (SONAR-3893) Improve the highlighter API to not depend on sonar-channel and allow to work on multi-line tokens - Full stack integration from RoR API --- .../java/org/sonar/core/source/HtmlTextWrapper.java | 5 ----- .../java/org/sonar/core/source/SyntaxHighlighter.java | 11 ++++++----- .../sonar/core/source/SyntaxHighlightingRuleSet.java | 2 +- .../WEB-INF/app/controllers/resource_controller.rb | 2 +- .../main/webapp/WEB-INF/app/helpers/source_helper.rb | 3 ++- .../src/main/webapp/WEB-INF/app/models/snapshot.rb | 4 ++++ 6 files changed, 14 insertions(+), 13 deletions(-) diff --git a/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java b/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java index d99aff3201a..62cf3bfdbf6 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java +++ b/sonar-core/src/main/java/org/sonar/core/source/HtmlTextWrapper.java @@ -38,9 +38,6 @@ import static org.sonar.core.source.CharactersReader.END_OF_STREAM; */ public class HtmlTextWrapper { - private static final String OPEN_TABLE_LINE = ""; - private static final String CLOSE_TABLE_LINE = ""; - public static final char CR_END_OF_LINE = '\r'; public static final char LF_END_OF_LINE = '\n'; @@ -61,7 +58,6 @@ public class HtmlTextWrapper { if (shouldStartNewLine(charsReader)) { decoratedHtmlLines.add(currentHtmlLine.toString()); currentHtmlLine = new StringBuilder(); -// currentHtmlLine.append(OPEN_TABLE_LINE); if (shouldReopenPendingTags(charsReader)) { reopenCurrentSyntaxTags(charsReader, currentHtmlLine); } @@ -72,7 +68,6 @@ public class HtmlTextWrapper { if (shouldClosePendingTags(charsReader)) { closeCurrentSyntaxTags(charsReader, currentHtmlLine); -// currentHtmlLine.append(CLOSE_TABLE_LINE); } Collection tagsToOpen = getTagsToOpen(charsReader.getCurrentIndex(), context); diff --git a/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlighter.java b/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlighter.java index c33a9fd4765..c9beafd8a94 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlighter.java +++ b/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlighter.java @@ -44,10 +44,11 @@ public class SyntaxHighlighter { String snapshotSource = snapshotSourceDao.selectSnapshotSource(snapshotId); String highlightingRules = snapshotDataDao.selectSnapshotData(snapshotId); - HighlightingContext highlightingContext = HighlightingContext.buildFrom(highlightingRules); - - HtmlTextWrapper textWrapper = new HtmlTextWrapper(); - - return textWrapper.wrapTextWithHtml(snapshotSource, highlightingContext); + if(snapshotSource != null && highlightingRules != null) { + HighlightingContext highlightingContext = HighlightingContext.buildFrom(highlightingRules); + HtmlTextWrapper textWrapper = new HtmlTextWrapper(); + return textWrapper.wrapTextWithHtml(snapshotSource, highlightingContext); + } + return null; } } diff --git a/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlightingRuleSet.java b/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlightingRuleSet.java index bbcc51a0fd6..b83c1b1b711 100644 --- a/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlightingRuleSet.java +++ b/sonar-core/src/main/java/org/sonar/core/source/SyntaxHighlightingRuleSet.java @@ -95,7 +95,7 @@ public class SyntaxHighlightingRuleSet { return false; } }); - return conflictingRules.size() > 0; + return !conflictingRules.isEmpty(); } } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/resource_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/resource_controller.rb index b74b6398e13..fbcb801ae98 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/resource_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/resource_controller.rb @@ -158,7 +158,7 @@ class ResourceController < ApplicationController @expanded=(params[:expand]=='true') @display_manual_violation_form=(current_user && has_role?(:user, @snapshot)) if @snapshot.source - source_lines=@snapshot.source.syntax_highlighted_lines() + source_lines = @snapshot.highlighting_data || @snapshot.source.syntax_highlighted_lines() init_scm() @lines=[] diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb index 4d618141afc..b1b3753f323 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/source_helper.rb @@ -45,7 +45,8 @@ module SourceHelper panel.html_lines=[] line_range=sanitize_line_range(options[:line_range]) highlighted_lines=options[:highlighted_lines]||[] - snapshot.source.syntax_highlighted_lines().each_with_index do |source, index| + source_lines = snapshot.highlighting_data || snapshot.source.syntax_highlighted_lines() + source_lines.each_with_index do |source, index| if line_range.include?(index+1) html_line=HtmlLine.new(source, index+1) html_line.revision=revisions_by_line[index+1] diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb index c03a6949ae4..01c70cb0f3a 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb @@ -252,6 +252,10 @@ class Snapshot < ActiveRecord::Base end end + def highlighting_data + Java::OrgSonarServerUi::JRubyFacade.getInstance().getHighlightedSourceLines(id) + end + private -- 2.39.5