@@ -13,7 +13,7 @@ import { markdownService } from "./MarkdownService";
13
13
import { ISubmitEvent } from "../model/Model" ;
14
14
import { IWebViewOption } from "../model/Model" ;
15
15
import { promptHintMessage } from "../utils/OutputUtils" ;
16
-
16
+ import { isAnswerDiffColor } from "../utils/ConfigUtils" ;
17
17
class SubmissionService extends BaseWebViewService {
18
18
protected readonly viewType : string = "leetcode.submission" ;
19
19
private result : IResult ;
@@ -34,28 +34,82 @@ class SubmissionService extends BaseWebViewService {
34
34
} ;
35
35
}
36
36
37
+ private sections_filtter ( key ) {
38
+ if ( key . substring ( 0 , 6 ) == "Output" ) {
39
+ return false ;
40
+ } else if ( key . substring ( 0 , 8 ) == "Expected" ) {
41
+ return false ;
42
+ } else if ( key == "messages" ) {
43
+ return false ;
44
+ } else if ( key == "system_message" ) {
45
+ return false ;
46
+ }
47
+ return true ;
48
+ }
49
+ private getAnswerKey ( result ) {
50
+ let ans ;
51
+ let exp ;
52
+ for ( const key in result ) {
53
+ if ( key . substring ( 0 , 6 ) == "Output" ) {
54
+ ans = key ;
55
+ } else if ( key . substring ( 0 , 8 ) == "Expected" ) {
56
+ exp = key ;
57
+ }
58
+ if ( ans != undefined && exp != undefined ) {
59
+ break ;
60
+ }
61
+ }
62
+ let key : Array < any > = [ ] ;
63
+ key . push ( ans ) ;
64
+ key . push ( exp ) ;
65
+ return key ;
66
+ }
67
+
37
68
protected getWebviewContent ( ) : string {
38
69
const styles : string = markdownService . getStyles ( ) ;
39
70
const title : string = `## ${ this . result . messages [ 0 ] } ` ;
40
71
const messages : string [ ] = this . result . messages . slice ( 1 ) . map ( ( m : string ) => `* ${ m } ` ) ;
41
- const sections : string [ ] = Object . keys ( this . result )
42
- . filter ( ( key : string ) => key !== "messages" && key !== "system_message" )
43
- . map ( ( key : string ) => [ `### ${ key } ` , "```" , this . result [ key ] . join ( "\n" ) , "```" ] . join ( "\n" ) ) ;
44
- const body : string = markdownService . render ( [ title , ...messages , ...sections ] . join ( "\n" ) ) ;
45
- return `
46
- <!DOCTYPE html>
47
- <html>
48
- <head>
49
- <meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
50
- <meta charset="UTF-8">
51
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
52
- ${ styles }
53
- </head>
54
- <body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
55
- ${ body }
56
- </body>
57
- </html>
58
- ` ;
72
+ let sections : string [ ] = [ ] ;
73
+ if ( isAnswerDiffColor ( ) ) {
74
+ sections = Object . keys ( this . result )
75
+ . filter ( this . sections_filtter )
76
+ . map ( ( key : string ) => [ `### ${ key } ` , "```" , this . result [ key ] . join ( "\n" ) , "```" ] . join ( "\n" ) ) ;
77
+
78
+ let ans_key : Array < any > = this . getAnswerKey ( this . result ) ;
79
+ if ( ans_key [ 0 ] != undefined && ans_key [ 1 ] != undefined ) {
80
+ sections . push ( `### Answer\n` ) ;
81
+ sections . push ( `| ${ ans_key [ 0 ] } | ${ ans_key [ 1 ] } | ` ) ;
82
+ sections . push ( `| :---------: | :---------: | ` ) ;
83
+ let ans = this . result [ ans_key [ 0 ] ] ;
84
+ let exp = this . result [ ans_key [ 1 ] ] ;
85
+ let max_len = Math . max ( ans . length , exp . length ) ;
86
+ for ( let index = 0 ; index < max_len ; index ++ ) {
87
+ sections . push ( `| ${ ans [ index ] || "" } | ${ exp [ index ] || "" } | ` ) ;
88
+ }
89
+ }
90
+ // require("../utils/testHot").test_add_table(sections);
91
+ } else {
92
+ sections = Object . keys ( this . result )
93
+ . filter ( ( key : string ) => key !== "messages" && key !== "system_message" )
94
+ . map ( ( key : string ) => [ `### ${ key } ` , "```" , this . result [ key ] . join ( "\n" ) , "```" ] . join ( "\n" ) ) ;
95
+ }
96
+ let body : string = markdownService . render ( [ title , ...messages , ...sections ] . join ( "\n" ) ) ;
97
+
98
+ let aaa = `
99
+ <!DOCTYPE html>
100
+ <html>
101
+ <head>
102
+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src https:; script-src vscode-resource:; style-src vscode-resource:;"/>
103
+ <meta charset="UTF-8">
104
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
105
+ ${ styles }
106
+ </head>
107
+ <body class="vscode-body 'scrollBeyondLastLine' 'wordWrap' 'showEditorSelection'" style="tab-size:4">
108
+ ${ body }
109
+ </body>
110
+ </html>
111
+ ` ;
112
+ return aaa ;
59
113
}
60
114
61
115
protected onDidDisposeWebview ( ) : void {
@@ -76,8 +130,70 @@ class SubmissionService extends BaseWebViewService {
76
130
await commands . executeCommand ( "workbench.action.openGlobalKeybindings" , query ) ;
77
131
}
78
132
133
+ private add_color_str ( str1 , str2 ) {
134
+ let result : Array < string > = [ ] ;
135
+ let min_len = Math . min ( str1 . length , str2 . length ) ;
136
+ let dif_len = 0 ;
137
+ for ( let index = 0 ; index < min_len ; index ++ ) {
138
+ if ( str1 [ index ] != str2 [ index ] ) {
139
+ dif_len = index ;
140
+ break ;
141
+ }
142
+ }
143
+ let str1_left = str1 . substring ( 0 , dif_len ) ;
144
+ let str1_right = str1 . substring ( dif_len ) ;
145
+ let str2_left = str2 . substring ( 0 , dif_len ) ;
146
+ let str2_right = str2 . substring ( dif_len ) ;
147
+ result . push ( str1_left + this . getRedPre ( ) + str1_right + this . getRedEnd ( ) ) ;
148
+ result . push ( str2_left + this . getRedPre ( ) + str2_right + this . getRedEnd ( ) ) ;
149
+
150
+ return result ;
151
+ }
152
+
153
+ private add_color ( temp ) {
154
+ // let;
155
+ let output_key ;
156
+ let expected_key ;
157
+ for ( const key in temp ) {
158
+ if ( typeof key == "string" ) {
159
+ if ( key . substring ( 0 , 6 ) == "Output" ) {
160
+ output_key = key ;
161
+ } else if ( key . substring ( 0 , 8 ) == "Expected" ) {
162
+ expected_key = key ;
163
+ }
164
+ if ( output_key && expected_key ) {
165
+ break ;
166
+ }
167
+ }
168
+ }
169
+ if ( output_key && expected_key ) {
170
+ let output_str = temp [ output_key ] || [ ] ;
171
+ let expected_str = temp [ expected_key ] || [ ] ;
172
+ let min_len = Math . min ( output_str . length , expected_str . length ) ;
173
+ for ( let index = 0 ; index < min_len ; index ++ ) {
174
+ if ( output_str [ index ] != expected_str [ index ] ) {
175
+ let temp_result = this . add_color_str ( output_str [ index ] , expected_str [ index ] ) ;
176
+ output_str [ index ] = temp_result [ 0 ] || "" ;
177
+ expected_str [ index ] = temp_result [ 1 ] || "" ;
178
+ }
179
+ }
180
+ }
181
+ }
182
+
183
+ private getRedPre ( ) {
184
+ return "__`" ;
185
+ }
186
+ private getRedEnd ( ) {
187
+ return "`__" ;
188
+ }
189
+
79
190
private parseResult ( raw : string ) : IResult {
80
- return JSON . parse ( raw ) ;
191
+ let temp = JSON . parse ( raw ) ;
192
+ if ( isAnswerDiffColor ( ) ) {
193
+ this . add_color ( temp ) ;
194
+ }
195
+
196
+ return temp ;
81
197
}
82
198
}
83
199
0 commit comments