Disable syntax highlighting for long lines in comment context Similar to diff-view if a line has length more than 500 chars, then turn off syntax highlighting for that context code. Change-Id: If4a12e50d2969e6f91531498ca575c39244715fe
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.ts b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.ts index 09e9d7c..b42deda 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.ts +++ b/polygerrit-ui/app/elements/diff/gr-diff-host/gr-diff-host.ts
@@ -23,10 +23,12 @@ GeneratedWebLink, } from '../../core/gr-navigation/gr-navigation'; import { + anyLineTooLong, getLine, getRange, getSide, rangesEqual, + SYNTAX_MAX_LINE_LENGTH, } from '../gr-diff/gr-diff-utils'; import {appContext} from '../../../services/app-context'; import { @@ -95,10 +97,6 @@ // Disable syntax highlighting if the overall diff is too large. const SYNTAX_MAX_DIFF_LENGTH = 20000; -// If any line of the diff is more than the character limit, then disable -// syntax highlighting for the entire file. -const SYNTAX_MAX_LINE_LENGTH = 500; - // 120 lines is good enough threshold for full-sized window viewport const NUM_OF_LINES_THRESHOLD_FOR_VIEWPORT = 120; @@ -1030,7 +1028,7 @@ if (!preferenceChangeRecord?.base?.syntax_highlighting || !diff) { return false; } - if (this._anyLineTooLong(diff)) { + if (anyLineTooLong(diff)) { fireAlert( this, `Files with line longer than ${SYNTAX_MAX_LINE_LENGTH} characters` + @@ -1049,20 +1047,6 @@ return true; } - /** - * @return whether any of the lines in diff are longer - * than SYNTAX_MAX_LINE_LENGTH. - */ - _anyLineTooLong(diff?: DiffInfo) { - if (!diff) return false; - return diff.content.some(section => { - const lines = section.ab - ? section.ab - : (section.a || []).concat(section.b || []); - return lines.some(line => line.length >= SYNTAX_MAX_LINE_LENGTH); - }); - } - _listenToViewportRender() { const renderUpdateListener: DiffLayerListener = start => { if (start > NUM_OF_LINES_THRESHOLD_FOR_VIEWPORT) {
diff --git a/polygerrit-ui/app/elements/diff/gr-diff/gr-diff-utils.ts b/polygerrit-ui/app/elements/diff/gr-diff/gr-diff-utils.ts index 96fbd8d..fada9cb 100644 --- a/polygerrit-ui/app/elements/diff/gr-diff/gr-diff-utils.ts +++ b/polygerrit-ui/app/elements/diff/gr-diff/gr-diff-utils.ts
@@ -18,6 +18,11 @@ import {CommentRange} from '../../../types/common'; import {FILE, LineNumber} from './gr-diff-line'; import {Side} from '../../../constants/constants'; +import {DiffInfo} from '../../../types/diff'; + +// If any line of the diff is more than the character limit, then disable +// syntax highlighting for the entire file. +export const SYNTAX_MAX_LINE_LENGTH = 500; /** * Compare two ranges. Either argument may be falsy, but will only return @@ -130,3 +135,17 @@ (node as Element).classList.contains('comment-thread') ); } + +/** + * @return whether any of the lines in diff are longer + * than SYNTAX_MAX_LINE_LENGTH. + */ +export function anyLineTooLong(diff?: DiffInfo) { + if (!diff) return false; + return diff.content.some(section => { + const lines = section.ab + ? section.ab + : (section.a || []).concat(section.b || []); + return lines.some(line => line.length >= SYNTAX_MAX_LINE_LENGTH); + }); +}
diff --git a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts index 4e953e8..cf8a0ed 100644 --- a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts +++ b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts
@@ -61,6 +61,7 @@ import {GrSyntaxLayer} from '../../diff/gr-syntax-layer/gr-syntax-layer'; import {StorageLocation} from '../../../services/storage/gr-storage'; import {TokenHighlightLayer} from '../../diff/gr-diff-builder/token-highlight-layer'; +import {anyLineTooLong} from '../../diff/gr-diff/gr-diff-utils'; const UNRESOLVED_EXPAND_COUNT = 5; const NEWLINE_PATTERN = /\n/g; @@ -245,15 +246,17 @@ get _diff() { if (this.comments === undefined || this.path === undefined) return; if (!this.comments[0]?.context_lines?.length) return; - waitForEventOnce(this, 'render').then(() => { - this.syntaxLayer.process(); - }); const diff = computeDiffFromContext( this.comments[0].context_lines, this.path, this.comments[0].source_content_type ); - this.syntaxLayer.init(diff); + if (!anyLineTooLong(diff)) { + this.syntaxLayer.init(diff); + waitForEventOnce(this, 'render').then(() => { + this.syntaxLayer.process(); + }); + } return diff; }