1
- // syntax highlighting based on https://github.com/dominictarr/ansi-highlight by the fantastic Dominic Tarr
2
-
3
- var repeating = require ( "repeating" ) ;
4
- var tokenize = require ( "js-tokenizer" ) ;
5
- var chalk = require ( "chalk" ) ;
1
+ var repeating = require ( "repeating" ) ;
2
+ var jsTokens = require ( "js-tokens" ) ;
3
+ var isJSKeyword = require ( "is-keyword-js" ) ;
4
+ var chalk = require ( "chalk" ) ;
5
+ var lineNumbers = require ( "line-numbers" ) ;
6
+ var ary = require ( "lodash/function/ary" ) ;
6
7
7
8
var defs = {
8
- string1 : " red" ,
9
- string2 : "red" ,
10
- punct : [ " white" , " bold" ] ,
11
- curly : " green" ,
12
- parens : [ "blue" , " bold" ] ,
13
- square : [ " yellow" ] ,
14
- name : " white" ,
15
- keyword : [ " cyan" ] ,
16
- number : " magenta" ,
17
- regexp : " magenta" ,
18
- comment1 : " grey" ,
19
- comment2 : "grey"
9
+ string : chalk . red ,
10
+ punctuation : chalk . white . bold ,
11
+ operator : chalk . white . bold ,
12
+ curly : chalk . green ,
13
+ parens : chalk . blue . bold ,
14
+ square : chalk . yellow ,
15
+ name : chalk . white ,
16
+ keyword : chalk . cyan ,
17
+ number : chalk . magenta ,
18
+ regex : chalk . magenta ,
19
+ comment : chalk . grey ,
20
+ invalid : chalk . inverse
20
21
} ;
21
22
22
- var highlight = function ( text ) {
23
- var colorize = function ( str , col ) {
24
- if ( ! col ) return str ;
23
+ var newline = / \r \n | [ \n \r \u2028 \u2029 ] / ;
25
24
26
- if ( Array . isArray ( col ) ) {
27
- col . forEach ( function ( col ) {
28
- str = chalk [ col ] ( str ) ;
29
- } ) ;
30
- } else {
31
- str = chalk [ col ] ( str ) ;
25
+ var highlight = function ( text ) {
26
+ var tokenType = function ( match ) {
27
+ var token = jsTokens . matchToToken ( match ) ;
28
+ if ( token . type === "name" && isJSKeyword ( token . value ) ) {
29
+ return "keyword" ;
32
30
}
33
- return str ;
31
+ if ( token . type === "punctuation" ) {
32
+ switch ( token . value ) {
33
+ case "{" :
34
+ case "}" :
35
+ return "curly" ;
36
+ case "(" :
37
+ case ")" :
38
+ return "parens" ;
39
+ case "[" :
40
+ case "]" :
41
+ return "square" ;
42
+ }
43
+ }
44
+ return token . type ;
34
45
} ;
35
46
36
- return tokenize ( text , true ) . map ( function ( str ) {
37
- var type = tokenize . type ( str ) ;
38
- return colorize ( str , defs [ type ] ) ;
39
- } ) . join ( "" ) ;
47
+ return text . replace ( jsTokens , function ( match ) {
48
+ var type = tokenType ( arguments ) ;
49
+ if ( type in defs ) {
50
+ var colorize = ary ( defs [ type ] , 1 ) ;
51
+ return match . split ( newline ) . map ( colorize ) . join ( "\n" ) ;
52
+ }
53
+ return match ;
54
+ } ) ;
40
55
} ;
41
56
42
57
module . exports = function ( lines , lineNumber , colNumber ) {
@@ -46,33 +61,29 @@ module.exports = function (lines, lineNumber, colNumber) {
46
61
lines = highlight ( lines ) ;
47
62
}
48
63
49
- lines = lines . split ( / \r \n | [ \n \r \u2028 \u2029 ] / ) ;
64
+ lines = lines . split ( newline ) ;
50
65
51
66
var start = Math . max ( lineNumber - 3 , 0 ) ;
52
67
var end = Math . min ( lines . length , lineNumber + 3 ) ;
53
- var width = ( end + "" ) . length ;
54
68
55
69
if ( ! lineNumber && ! colNumber ) {
56
70
start = 0 ;
57
71
end = lines . length ;
58
72
}
59
73
60
- return "\n" + lines . slice ( start , end ) . map ( function ( line , i ) {
61
- var curr = i + start + 1 ;
62
-
63
- var gutter = curr === lineNumber ? "> " : " " ;
64
-
65
- var sep = curr + repeating ( " " , width + 1 ) ;
66
- gutter += sep + "| " ;
67
-
68
- var str = gutter + line ;
69
-
70
- if ( colNumber && curr === lineNumber ) {
71
- str += "\n" ;
72
- str += repeating ( " " , gutter . length - 2 ) ;
73
- str += "|" + repeating ( " " , colNumber ) + "^" ;
74
+ return "\n" + lineNumbers ( lines . slice ( start , end ) , {
75
+ start : start + 1 ,
76
+ before : " " ,
77
+ after : " | " ,
78
+ transform : function ( params ) {
79
+ if ( params . number !== lineNumber ) {
80
+ return ;
81
+ }
82
+ if ( colNumber ) {
83
+ params . line += "\n" + params . before + repeating ( " " , params . width ) +
84
+ params . after + repeating ( " " , colNumber - 1 ) + "^" ;
85
+ }
86
+ params . before = params . before . replace ( / ^ ./ , ">" ) ;
74
87
}
75
-
76
- return str ;
77
88
} ) . join ( "\n" ) ;
78
89
} ;
0 commit comments