Skip to content

Commit 196c84c

Browse files
author
Guillaume Chau
committed
feat(file-diff): syntax highlighting, better background colors
1 parent 9079d3e commit 196c84c

File tree

8 files changed

+170
-17
lines changed

8 files changed

+170
-17
lines changed

packages/@vue/cli-ui/apollo-server/connectors/git.js

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const execa = require('execa')
22
const path = require('path')
3+
const fs = require('fs-extra')
34
const parseDiff = require('../util/parse-diff')
5+
const { highlightCode } = require('../util/highlight')
46
// Connectors
57
const cwd = require('./cwd')
68
// Utils
@@ -34,13 +36,41 @@ async function getDiffs (context) {
3436
cwd: cwd.get()
3537
})
3638
await reset(context)
37-
const list = parseDiff(stdout).map(
38-
fileDiff => ({
39+
let list = parseDiff(stdout)
40+
for (const n in list) {
41+
const fileDiff = list[n]
42+
const isNew = newFiles.includes(fileDiff.to)
43+
let fromContent
44+
if (!isNew) {
45+
const result = await execa('git', ['show', `HEAD:${fileDiff.from}`], {
46+
cwd: cwd.get()
47+
})
48+
fromContent = result.stdout
49+
}
50+
const highlightedContentFrom = fromContent && highlightCode(fileDiff.from, fromContent).split('\n')
51+
const highlightedContentTo = highlightCode(fileDiff.to, fs.readFileSync(path.resolve(cwd.get(), fileDiff.to), { encoding: 'utf8' })).split('\n')
52+
for (const chunk of fileDiff.chunks) {
53+
for (const change of chunk.changes) {
54+
const firstChar = change.content.charAt(0)
55+
let highlightedCode
56+
if (change.normal) {
57+
highlightedCode = highlightedContentFrom[change.ln1 - 1]
58+
} else if (change.type === 'del') {
59+
highlightedCode = highlightedContentFrom[change.ln - 1]
60+
} else if (change.type === 'add') {
61+
highlightedCode = highlightedContentTo[change.ln - 1]
62+
}
63+
if (highlightedCode) {
64+
change.content = firstChar + highlightedCode
65+
}
66+
}
67+
}
68+
list[n] = {
3969
id: fileDiff.index.join(' '),
4070
...fileDiff,
41-
new: newFiles.includes(fileDiff.to)
42-
})
43-
)
71+
new: isNew
72+
}
73+
}
4474

4575
return list
4676
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const Prism = require('prismjs')
2+
const loadLanguages = require('prismjs/components/')
3+
const path = require('path')
4+
5+
loadLanguages()
6+
7+
const languages = [
8+
{ test: /\.(html|vue|xml)$/, lang: 'markup' },
9+
{ test: /\.js$/, lang: 'javascript' },
10+
{ test: /\.sh$/, lang: 'bash' },
11+
{ test: /\.coffee$/, lang: 'coffeescript' },
12+
{ test: /\.gql$/, lang: 'graphql' },
13+
{ test: /\.hx$/, lang: 'haxe' },
14+
{ test: /\.md$/, lang: 'markdown' },
15+
{ test: /\.py$/, lang: 'python' },
16+
{ test: /\.rb$/, lang: 'ruby' },
17+
{ test: /\.styl$/, lang: 'stylus' },
18+
{ test: /\.ts$/, lang: 'typescript' },
19+
{ test: /\.yml$/, lang: 'yaml' }
20+
]
21+
22+
exports.highlightCode = function (filename, content, lang = null) {
23+
let language
24+
if (lang) {
25+
language = { lang }
26+
}
27+
if (!language) {
28+
language = languages.find(l => l.test.test(filename))
29+
}
30+
if (!language) {
31+
const ext = path.extname(filename).substr(1)
32+
if (Prism.languages[ext]) {
33+
language = { lang: ext }
34+
}
35+
}
36+
// No language found
37+
if (!language) return content
38+
// Highlight code
39+
return Prism.highlight(content, Prism.languages[language.lang], language.lang)
40+
}

packages/@vue/cli-ui/package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@vue/cli-ui/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"node-notifier": "^5.2.1",
4949
"parse-git-config": "^2.0.2",
5050
"portfinder": "^1.0.13",
51+
"prismjs": "^1.15.0",
5152
"semver": "^5.5.0",
5253
"shortid": "^2.2.11",
5354
"terminate": "^2.1.0",

packages/@vue/cli-ui/src/components/FileDiff.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ status-color($color)
136136
.name
137137
flex auto 1 0
138138
font-family $font-mono
139-
font-size 12px
139+
font-size 14px
140+
font-weight bold
140141
ellipsis()
141142
&.from-file
142143
text-decoration line-through

packages/@vue/cli-ui/src/components/FileDiffChange.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
{{ ln2 }}
2020
</div>
2121
</div>
22-
<div class="content">{{ change.content }}</div>
22+
<div class="content" v-html="change.content"/>
2323
</div>
2424
</template>
2525

@@ -114,21 +114,21 @@ export default {
114114
white-space pre
115115
116116
&.type-add
117-
background lighten($vue-ui-color-success, 80%)
117+
background desaturate(lighten($vue-ui-color-success, 90%), 30%)
118118
.vue-ui-dark-mode &
119-
background darken($vue-ui-color-success, 70%)
119+
background desaturate(darken($vue-ui-color-success, 60%), 50%)
120120
.lines
121-
background lighten($vue-ui-color-success, 60%)
121+
background lighten($vue-ui-color-success, 80%)
122122
.vue-ui-dark-mode &
123123
background darken($vue-ui-color-success, 60%)
124124
125125
&.type-del
126-
background lighten($vue-ui-color-danger, 80%)
126+
background desaturate(lighten($vue-ui-color-danger, 90%), 30%)
127127
.vue-ui-dark-mode &
128-
background darken($vue-ui-color-danger, 70%)
128+
background desaturate(darken($vue-ui-color-danger, 60%), 50%)
129129
130130
.lines
131-
background lighten($vue-ui-color-danger, 60%)
131+
background lighten($vue-ui-color-danger, 80%)
132132
.vue-ui-dark-mode &
133133
background darken($vue-ui-color-danger, 60%)
134134

packages/@vue/cli-ui/src/components/FileDiffChunk.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export default {
3434
&::after
3535
content '•••'
3636
height 44px
37-
background lighten($vue-ui-color-light-neutral, 30%)
37+
background $md-grey-100
3838
h-box()
3939
box-center()
40-
color darken($vue-ui-color-light-neutral, 30%)
40+
color $md-grey-300
4141
letter-spacing 4px
4242
.vue-ui-dark-mode &
4343
background lighten($vue-ui-color-darker, 1%)

packages/@vue/cli-ui/src/style/main.styl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
@import "imports"
1+
@import '~prismjs/themes/prism.css'
2+
@import 'imports'
23

34
html,
45
body,
@@ -159,4 +160,27 @@ ansi-colors('white', $vue-ui-color-light)
159160
&.anchor
160161
align-items flex-start
161162
.shell
162-
margin-top 42px
163+
margin-top 42px
164+
165+
// Syntax highlighting
166+
.token.operator, .token.entity, .token.url, .language-css .token.string, .style .token.string
167+
background none !important
168+
169+
.vue-ui-dark-mode
170+
.token.property, .token.tag, .token.boolean, .token.number, .token.constant, .token.symbol, .token.deleted
171+
color lighten(#905, 40%)
172+
173+
.token.selector, .token.attr-name, .token.string, .token.char, .token.builtin, .token.inserted
174+
color lighten(#690, 40%)
175+
176+
.token.atrule, .token.attr-value, .token.keyword
177+
color lighten(#07a, 40%)
178+
179+
.token.function, .token.class-name
180+
color lighten(#dd4a68, 30%)
181+
182+
.token.operator, .token.entity, .token.url, .language-css .token.string, .style .token.string
183+
color lighten(#9a6e3a, 20%)
184+
185+
.token.punctuation
186+
color lighten(#999, 20%)

0 commit comments

Comments
 (0)