-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcite-manager.coffee
167 lines (147 loc) · 4.72 KB
/
cite-manager.coffee
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
{watchPath, CompositeDisposable} = require 'atom'
promisify = require "promisify-node"
fs = promisify('fs')
glob = require 'glob'
path = require 'path'
Fuse = require 'fuse.js'
bibtexParse = require './lite-bibtex-parse'
referenceTools = require './reference-tools'
module.exports =
class CiteManager
fuseOptions =
shouldSort: true,
threshold: 0.6,
location: 0,
distance: 100,
maxPatternLength: 32,
minMatchCharLength: 1,
keys: [{
"name": "title",
"weight": 0.3
},
{
"name": "author.family",
"weight": 0.6
},
{
"name": "author.given",
"weight": 0.6
},
{
"name": "id",
"weight": 0.1
}]
constructor: ->
@disposables = new CompositeDisposable
@database = {}
@globalPathWatcher = undefined
@fuse = new Fuse(Object.values(@database),fuseOptions)
handleWatcherEvents: (events) =>
# Filter for bib files
events = events.filter (e) -> /bib$/.test(e.path)
# Filter multiple events for one file
flags = {}
events = events.reverse().filter (e) ->
if flags[e.path]
return false
flags[e.path] = true
return true
for e in events
switch e.action
when "created"
@addBibtexFile(e.path)
when "modified"
@addBibtexFile(e.path)
when "deleted"
@removeBibtexFile(e.path)
when "renamed"
@addBibtexFile(e.path)
initialize: ->
# Add Bibfiles to the Database
promises = []
for ppath in atom.project.getPaths()
promises.push(@addFilesFromFolder(ppath))
promises.push(atom.project.getWatcherPromise(ppath))
# Init the Path watcher
watcher = atom.project.onDidChangeFiles((events) =>
@handleWatcherEvents(events))
@disposables.add watcher
# handle global Path
if atom.config.get('autocomplete-latex-cite.includeGlobalBibFiles')
if atom.config.get('autocomplete-latex-cite.globalBibPath')
promises.push(@addGlobalBibFiles())
# Subscripe to events
@subscripeForConfigChanges()
return Promise.all(promises)
subscripeForConfigChanges: () ->
atom.config.onDidChange 'autocomplete-latex-cite.includeGlobalBibFiles', ({newValue, oldValue}) =>
if newValue
if atom.config.get('autocomplete-latex-cite.globalBibPath')
@addGlobalBibFiles()
else
@removeGlobalBibFiles()
atom.config.onDidChange 'autocomplete-latex-cite.globalBibPath', ({newValue, oldValue}) =>
if newValue
if atom.config.get('autocomplete-latex-cite.includeGlobalBibFiles')
@removeGlobalBibFiles()
@addGlobalBibFiles()
else
@removeGlobalBibFiles()
addGlobalBibFiles: () ->
return new Promise ( (resolve) =>
globalPath = atom.config.get('autocomplete-latex-cite.globalBibPath')
@addFilesFromFolder(globalPath).then( (result) =>
watchPath(globalPath,{},( (events) =>
@handleWatcherEvents(events))).then ( (watcherDisposal) =>
@disposables.add watcherDisposal
@globalPathWatcher = watcherDisposal
resolve(result)
)
)
)
removeGlobalBibFiles: () ->
if @globalPathWatcher
files = glob.sync(path.join(@globalPathWatcher.watchedPath, '**/*.bib'))
for file in files
@removeBibtexFile(file)
@globalPathWatcher.dispose()
@disposables.remove @globalPathWatcher
@globalPathWatcher = undefined
addFilesFromFolder: (folder) ->
files = glob.sync(path.join(folder, '**/*.bib'))
promises = []
for file in files
promises.push(@addBibtexFile(file))
return Promise.all(promises)
destroy: () ->
@disposables.dispose()
removeBibtexFile: (file) ->
# Remove Database Entries for File
for key,value of @database
if value.sourcefile is file
delete @database[key]
@fuse = new Fuse(Object.values(@database),fuseOptions)
addBibtexFile: (file) ->
return new Promise((resolve, reject) =>
fs.readFile(file, 'utf8').then( (content) =>
bibtex = bibtexParse.toJSON(content)
bibtex = referenceTools.enhanceReferences(bibtex)
for el in bibtex
el['sourcefile'] = file
@database[el['id']] = el
@fuse = new Fuse(Object.values(@database),fuseOptions)
resolve(@database)
).catch( (error) ->
message = "Autocomplete Latex Cite Warning"
options = {
'dismissable': true
'description': """Unable to parse Bibtex file #{file}. It will be
ignored for autocompletion. (`#{error.message}`)
"""
}
atom.notifications.addWarning(message, options)
resolve(@database)
)
)
searchForPrefixInDatabase: (prefix) ->
@fuse.search(prefix)