Skip to content

Commit 7cb0dc8

Browse files
author
Guillaume Chau
committed
feat(ui): FileDiff open in editor
1 parent 0a53836 commit 7cb0dc8

File tree

9 files changed

+144
-5
lines changed

9 files changed

+144
-5
lines changed

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020

2121
<div class="vue-ui-spacer"/>
2222

23+
<VueButton
24+
v-if="!fileDiff.deleted"
25+
icon-left="edit"
26+
class="icon-button"
27+
v-tooltip="$t('components.file-diff.actions.open')"
28+
@click.stop="openInEditor()"
29+
/>
30+
2331
<VueButton
2432
:icon-left="collapsed ? 'keyboard_arrow_down' : 'keyboard_arrow_up'"
2533
class="icon-button"
@@ -43,7 +51,18 @@
4351
</template>
4452

4553
<script>
54+
import FILE_OPEN_IN_EDITOR from '../graphql/fileOpenInEditor.gql'
55+
4656
export default {
57+
provide () {
58+
const vm = this
59+
return {
60+
FileDiffInjection: {
61+
get data () { return vm.fileDiff }
62+
}
63+
}
64+
},
65+
4766
props: {
4867
fileDiff: {
4968
type: Object,
@@ -65,6 +84,20 @@ export default {
6584
}
6685
return 'insert_drive_file'
6786
}
87+
},
88+
89+
methods: {
90+
openInEditor () {
91+
this.$apollo.mutate({
92+
mutation: FILE_OPEN_IN_EDITOR,
93+
variables: {
94+
input: {
95+
file: this.fileDiff.to,
96+
gitPath: true
97+
}
98+
}
99+
})
100+
}
68101
}
69102
}
70103
</script>

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
<div class="ln ln1">
1010
{{ ln1 }}
1111
</div>
12-
<div class="ln ln2">
12+
<div
13+
:class="{
14+
disabled: !ln2
15+
}"
16+
class="ln ln2"
17+
@click="openInEditor()"
18+
>
1319
{{ ln2 }}
1420
</div>
1521
</div>
@@ -18,7 +24,13 @@
1824
</template>
1925

2026
<script>
27+
import FILE_OPEN_IN_EDITOR from '../graphql/fileOpenInEditor.gql'
28+
2129
export default {
30+
inject: [
31+
'FileDiffInjection'
32+
],
33+
2234
props: {
2335
change: {
2436
type: Object,
@@ -42,6 +54,23 @@ export default {
4254
return this.change.ln
4355
}
4456
}
57+
},
58+
59+
methods: {
60+
openInEditor () {
61+
if (!this.ln2) return
62+
63+
this.$apollo.mutate({
64+
mutation: FILE_OPEN_IN_EDITOR,
65+
variables: {
66+
input: {
67+
file: this.FileDiffInjection.data.to,
68+
line: this.ln2,
69+
gitPath: true
70+
}
71+
}
72+
})
73+
}
4574
}
4675
}
4776
</script>
@@ -70,6 +99,13 @@ export default {
7099
width 0
71100
overflow hidden
72101
102+
.ln2
103+
cursor pointer
104+
&:hover
105+
text-decoration underline
106+
&.disabled
107+
pointer-events none
108+
73109
.content
74110
flex auto 1 1
75111
white-space pre
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const launch = require('launch-editor')
2+
const path = require('path')
3+
// Connectors
4+
const cwd = require('./cwd')
5+
const git = require('./git')
6+
const logs = require('./logs')
7+
8+
async function openInEditor (input, context) {
9+
let query
10+
if (input.gitPath) {
11+
query = await git.resolveFile(input.file, context)
12+
} else {
13+
path.resolve(cwd.get(), input.file)
14+
}
15+
if (input.line) {
16+
query += `:${input.line}`
17+
if (input.column) {
18+
query += `:${input.column}`
19+
}
20+
}
21+
logs.add({
22+
message: `Opening file '${query}' in code editor...`,
23+
type: 'info'
24+
}, context)
25+
launch(query, (fileName, errorMessage) => {
26+
console.error(`Unable to open '${fileName}'`, errorMessage)
27+
})
28+
return true
29+
}
30+
31+
module.exports = {
32+
openInEditor
33+
}

packages/@vue/cli-ui/src/graphql-api/connectors/git.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const execa = require('execa')
2+
const path = require('path')
23
const parseDiff = require('../utils/parse-diff')
34
// Connectors
45
const cwd = require('./cwd')
@@ -55,8 +56,25 @@ async function reset (context) {
5556
return true
5657
}
5758

59+
async function getRoot (context) {
60+
const { stdout } = await execa('git', [
61+
'rev-parse',
62+
'--show-toplevel'
63+
], {
64+
cwd: cwd.get()
65+
})
66+
return stdout
67+
}
68+
69+
async function resolveFile (file, context) {
70+
const root = await getRoot(context)
71+
return path.resolve(root, file)
72+
}
73+
5874
module.exports = {
5975
getDiffs,
6076
commit,
61-
reset
77+
reset,
78+
getRoot,
79+
resolveFile
6280
}

packages/@vue/cli-ui/src/graphql-api/resolvers.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const plugins = require('./connectors/plugins')
1313
const tasks = require('./connectors/tasks')
1414
const configurations = require('./connectors/configurations')
1515
const git = require('./connectors/git')
16+
const files = require('./connectors/files')
1617

1718
// Prevent code from exiting server process
1819
exit.exitProcess = false
@@ -90,7 +91,8 @@ module.exports = {
9091
taskLogsClear: (root, { id }, context) => tasks.clearLogs(id, context),
9192
configurationSave: (root, { id }, context) => configurations.save(id, context),
9293
configurationCancel: (root, { id }, context) => configurations.cancel(id, context),
93-
gitCommit: (root, { message }, context) => git.commit(message, context)
94+
gitCommit: (root, { message }, context) => git.commit(message, context),
95+
fileOpenInEditor: (root, { input }, context) => files.openInEditor(input, context)
9496
},
9597

9698
Subscription: {

packages/@vue/cli-ui/src/graphql-api/type-defs.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ enum FileDiffChangeType {
242242
del
243243
}
244244
245+
input OpenInEditorInput {
246+
file: String!
247+
line: Int
248+
column: Int
249+
gitPath: Boolean
250+
}
251+
245252
type Query {
246253
progress (id: ID!): Progress
247254
cwd: String!
@@ -286,6 +293,7 @@ type Mutation {
286293
configurationSave (id: ID!): Configuration
287294
configurationCancel (id: ID!): Configuration
288295
gitCommit (message: String!): Boolean
296+
fileOpenInEditor (input: OpenInEditorInput!): Boolean
289297
}
290298
291299
type Subscription {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mutation fileOpenInEditor ($input: OpenInEditorInput!) {
2+
fileOpenInEditor (input: $input)
3+
}

packages/@vue/cli-ui/src/locales/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"components": {
33
"file-diff": {
4-
"binary": "Binary file not shown"
4+
"binary": "Binary file not shown",
5+
"actions": {
6+
"open": "Open in editor"
7+
}
58
},
69
"file-diff-view": {
710
"files-changed": "Files changed",

packages/@vue/cli-ui/src/locales/fr.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"components": {
33
"file-diff": {
4-
"binary": "Fichier binaire non affiché"
4+
"binary": "Fichier binaire non affiché",
5+
"actions": {
6+
"open": "Open in editor"
7+
}
58
},
69
"file-diff-view": {
710
"files-changed": "Fichiers modifiés",

0 commit comments

Comments
 (0)