This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
/
index.js
119 lines (106 loc) · 3.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/** @babel */
function getPythonEnvironment () {
const {delimiter} = require('path')
const {env} = process
const pythonPath = atom.config.get('atom-isort.pythonPath')
let paths = []
let envPath = null
if (/^win/.test(process.platform)) {
paths = [
'C:\\Python2.7',
'C:\\Python3.4',
'C:\\Python34',
'C:\\Python3.5',
'C:\\Python35',
'C:\\Program Files (x86)\\Python 2.7',
'C:\\Program Files (x86)\\Python 3.4',
'C:\\Program Files (x86)\\Python 3.5',
'C:\\Program Files (x64)\\Python 2.7',
'C:\\Program Files (x64)\\Python 3.4',
'C:\\Program Files (x64)\\Python 3.5',
'C:\\Program Files\\Python 2.7',
'C:\\Program Files\\Python 3.4',
'C:\\Program Files\\Python 3.5'
]
envPath = (env.Path || '')
} else {
paths = ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']
envPath = (env.PATH || '')
}
envPath = envPath.split(delimiter)
envPath.unshift(pythonPath && !Array.from(envPath).includes(pythonPath) ? pythonPath : undefined)
for (let p of Array.from(paths)) {
if (!Array.from(envPath).includes(p)) envPath.push(p)
}
env.PATH = envPath.join(delimiter)
return env
}
export default {
config: require('./config.coffee').config,
activate () {
if (atom.inDevMode()) console.log('activate atom-isort')
require('atom-package-deps').install('atom-isort')
const Sorter = require('./sorter')
this.sorter = new Sorter(getPythonEnvironment())
this.handleEvents()
},
handleEvents () {
const {CompositeDisposable} = require('atom')
this.subs = new CompositeDisposable()
this.editorSubs = new CompositeDisposable()
this.subs.add(
atom.commands.add('atom-text-editor', 'atom-isort:sort imports', () => {
this.sorter.sortImports(atom.workspace.getActiveTextEditor())
})
)
this.subs.add(atom.config.observe('atom-isort.sortOnSave', (sortOnSave) => {
this.editorSubs.add(atom.workspace.observeTextEditors((textEditor) => {
if (sortOnSave) {
textEditor._isortSortOnWillSave =
textEditor.buffer.onWillSave(() => {
if (textEditor.getGrammar().scopeName !== 'source.python') return
this.sorter.sortImports(textEditor, true)
})
} else {
__guard__(textEditor._isortSortOnWillSave, x => x.dispose())
}
}))
}))
},
provideLinter () {
let getSorter = () => this.sorter
return {
name: 'isort',
scope: 'file',
lintsOnChange: false,
grammarScopes: ['source.python'],
async lint (textEditor) {
const sorted = getSorter().checkImports(textEditor)
if (sorted) return []
return [{
severity: 'warning',
location: {
file: textEditor.getPath(),
position: [[0, 0], [0, 0]]
},
excerpt: 'Imports not sorted'
}]
}
}
},
deactivate () {
for (let editor of Array.from(atom.workspace.getTextEditors())) {
__guard__(editor._isortSortOnWillSave, x => x.dispose())
}
__guard__(this.editorSubs, x => x.dispose())
this.editorSubs = null
__guard__(this.subs, x => x.dispose())
this.subs = null
this.sorter = null
}
}
function __guard__ (value, transform) {
return (typeof value !== 'undefined' && value !== null)
? transform(value)
: undefined
}