Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/changes/ChangesHighlighter.js

Issue 2780713005: DevTools: Add syntax highlighting to ChangesView (Closed)
Patch Set: diffRows Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/changes/ChangesView.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * @param {!Object} config 6 * @param {!Object} config
7 * @param {{rows: !Array<!Changes.ChangesView.Row>}} parserConfig 7 * @param {{diffRows: !Array<!Changes.ChangesView.Row>, baselineLines: !Array<st ring>, currentLines: !Array<string>, mimeType: string}} parserConfig
8 * @return {{ 8 * @return {{
9 * startState: function():!Changes.ChangesHighlighter.DiffState, 9 * startState: function():!Changes.ChangesHighlighter.DiffState,
10 * token: function({next: function()}, !Changes.ChangesHighlighter.DiffState):s tring, 10 * token: function(!CodeMirror.StringStream, !Changes.ChangesHighlighter.DiffSt ate):string,
11 * blankLine: function(!Changes.ChangesHighlighter.DiffState):string, 11 * blankLine: function(!Changes.ChangesHighlighter.DiffState):string,
12 * copyState: function(!Changes.ChangesHighlighter.DiffState):Changes.ChangesHi ghlighter.DiffState
12 * }} 13 * }}
13 */ 14 */
14 Changes.ChangesHighlighter = function(config, parserConfig) { 15 Changes.ChangesHighlighter = function(config, parserConfig) {
15 var rows = parserConfig.rows; 16 var diffRows = parserConfig.diffRows;
17 var baselineLines = parserConfig.baselineLines;
18 var currentLines = parserConfig.currentLines;
19 var syntaxHighlightMode = CodeMirror.getMode({}, parserConfig.mimeType);
20
21 /**
22 * @param {!Changes.ChangesHighlighter.DiffState} state
23 * @param {number} baselineLineNumber
24 * @param {number} currentLineNumber
25 */
26 function fastForward(state, baselineLineNumber, currentLineNumber) {
27 if (baselineLineNumber > state.baselineLineNumber) {
28 fastForwardSyntaxHighlighter(state.baselineSyntaxState, state.baselineLine Number, baselineLineNumber, baselineLines);
29 state.baselineLineNumber = baselineLineNumber;
30 }
31 if (currentLineNumber > state.currentLineNumber) {
32 fastForwardSyntaxHighlighter(state.currentSyntaxState, state.currentLineNu mber, currentLineNumber, currentLines);
33 state.currentLineNumber = currentLineNumber;
34 }
35 }
36
37 /**
38 * @param {!Object} syntaxState
39 * @param {number} from
40 * @param {number} to
41 * @param {!Array<string>} lines
42 */
43 function fastForwardSyntaxHighlighter(syntaxState, from, to, lines) {
44 var lineNumber = from;
45 while (lineNumber < to && lineNumber < lines.length) {
46 var stream = new CodeMirror.StringStream(lines[lineNumber]);
47 if (stream.eol() && syntaxHighlightMode.blankLine)
48 syntaxHighlightMode.blankLine(syntaxState);
49 while (!stream.eol()) {
50 syntaxHighlightMode.token(stream, syntaxState);
51 stream.start = stream.pos;
52 }
53 lineNumber++;
54 }
55 }
16 56
17 return { 57 return {
18 /** 58 /**
19 * @return {!Changes.ChangesHighlighter.DiffState} 59 * @return {!Changes.ChangesHighlighter.DiffState}
20 */ 60 */
21 startState: function() { 61 startState: function() {
22 return {lineNumber: 0, index: 0}; 62 return {
63 rowNumber: 0,
64 diffTokenIndex: 0,
65 currentLineNumber: 0,
66 baselineLineNumber: 0,
67 currentSyntaxState: CodeMirror.startState(syntaxHighlightMode),
68 baselineSyntaxState: CodeMirror.startState(syntaxHighlightMode),
69 syntaxPosition: 0,
70 diffPosition: 0,
71 syntaxStyle: '',
72 diffStyle: ''
73 };
23 }, 74 },
24 75
25 /** 76 /**
26 * @param {!{next: function()}} stream 77 * @param {!CodeMirror.StringStream} stream
27 * @param {!Changes.ChangesHighlighter.DiffState} state 78 * @param {!Changes.ChangesHighlighter.DiffState} state
28 * @return {string} 79 * @return {string}
29 */ 80 */
30 token: function(stream, state) { 81 token: function(stream, state) {
31 var row = rows[state.lineNumber]; 82 var diffRow = diffRows[state.rowNumber];
32 if (!row) { 83 if (!diffRow) {
33 stream.next(); 84 stream.next();
34 return ''; 85 return '';
35 } 86 }
87 fastForward(state, diffRow.baselineLineNumber - 1, diffRow.currentLineNumb er - 1);
36 var classes = ''; 88 var classes = '';
37 if (state.index === 0) 89 if (stream.pos === 0)
38 classes += ' line-background-' + row.type + ' line-' + row.type; 90 classes += ' line-background-' + diffRow.type + ' line-' + diffRow.type;
39 stream.pos += row.content[state.index].text.length; 91
40 classes += ' ' + row.content[state.index].className; 92 var syntaxHighlighterNeedsRefresh = state.diffPosition >= state.syntaxPosi tion;
41 state.index++; 93 if (state.diffPosition <= state.syntaxPosition) {
42 if (state.index >= row.content.length) { 94 state.diffPosition += diffRow.tokens[state.diffTokenIndex].text.length;
43 state.lineNumber++; 95 state.diffStyle = diffRow.tokens[state.diffTokenIndex].className;
44 state.index = 0; 96 state.diffTokenIndex++;
97 }
98
99 if (syntaxHighlighterNeedsRefresh) {
100 if (diffRow.type === Changes.ChangesView.RowType.Deletion || diffRow.typ e === Changes.ChangesView.RowType.Addition ||
101 diffRow.type === Changes.ChangesView.RowType.Equal) {
102 state.syntaxStyle = syntaxHighlightMode.token(
103 stream, diffRow.type === Changes.ChangesView.RowType.Deletion ? st ate.baselineSyntaxState : state.currentSyntaxState);
104 state.syntaxPosition = stream.pos;
105 } else {
106 state.syntaxStyle = '';
107 state.syntaxPosition = Infinity;
108 }
109 }
110
111 stream.pos = Math.min(state.syntaxPosition, state.diffPosition);
112 classes += ' ' + state.syntaxStyle;
113 classes += ' ' + state.diffStyle;
114
115 if (stream.eol()) {
116 state.rowNumber++;
117 if (diffRow.type === Changes.ChangesView.RowType.Deletion)
118 state.baselineLineNumber++;
119 else
120 state.currentLineNumber++;
121 state.diffPosition = 0;
122 state.syntaxPosition = 0;
123 state.diffTokenIndex = 0;
45 } 124 }
46 return classes; 125 return classes;
47 }, 126 },
48 127
49 /** 128 /**
50 * @param {!Changes.ChangesHighlighter.DiffState} state 129 * @param {!Changes.ChangesHighlighter.DiffState} state
51 * @return {string} 130 * @return {string}
52 */ 131 */
53 blankLine: function(state) { 132 blankLine: function(state) {
54 var row = rows[state.lineNumber]; 133 var diffRow = diffRows[state.rowNumber];
55 state.lineNumber++; 134 state.rowNumber++;
56 state.index = 0; 135 state.syntaxPosition = 0;
57 if (!row) 136 state.diffPosition = 0;
137 state.diffTokenIndex = 0;
138 if (!diffRow)
58 return ''; 139 return '';
59 return 'line-background-' + row.type + ' line-' + row.type; 140
141 var style = '';
142 if (syntaxHighlightMode.blankLine) {
143 if (diffRow.type === Changes.ChangesView.RowType.Equal || diffRow.type = == Changes.ChangesView.RowType.Addition) {
144 style = syntaxHighlightMode.blankLine(state.currentSyntaxState);
145 state.currentLineNumber++;
146 } else if (diffRow.type === Changes.ChangesView.RowType.Deletion) {
147 style = syntaxHighlightMode.blankLine(state.baselineSyntaxState);
148 state.baselineLineNumber++;
149 }
150 }
151 return style + ' line-background-' + diffRow.type + ' line-' + diffRow.typ e;
152 },
153
154 /**
155 * @param {!Changes.ChangesHighlighter.DiffState} state
156 * @return {!Changes.ChangesHighlighter.DiffState}
157 */
158 copyState: function(state) {
159 var newState = Object.assign({}, state);
160 newState.currentSyntaxState = CodeMirror.copyState(syntaxHighlightMode, st ate.currentSyntaxState);
161 newState.baselineSyntaxState = CodeMirror.copyState(syntaxHighlightMode, s tate.baselineSyntaxState);
162 return /** @type {!Changes.ChangesHighlighter.DiffState} */ (newState);
60 } 163 }
61 }; 164 };
62 }; 165 };
63 166
64 /** @typedef {!{lineNumber: number, index: number}} */ 167 /**
168 * @typedef {!{
169 * rowNumber: number,
170 * diffTokenIndex: number,
171 * currentLineNumber: number,
172 * baselineLineNumber: number,
173 * currentSyntaxState: !Object,
174 * baselineSyntaxState: !Object,
175 * syntaxPosition: number,
176 * diffPosition: number,
177 * syntaxStyle: string,
178 * diffStyle: string
179 * }}
180 */
65 Changes.ChangesHighlighter.DiffState; 181 Changes.ChangesHighlighter.DiffState;
66 182
67 CodeMirror.defineMode('devtools-diff', Changes.ChangesHighlighter); 183 CodeMirror.defineMode('devtools-diff', Changes.ChangesHighlighter);
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/changes/ChangesView.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698