mirror of
https://github.com/James-Yu/LaTeX-Workshop.git
synced 2024-10-04 15:17:59 +03:00
Add env and ref autocompletion
This commit is contained in:
parent
ff289b010b
commit
6a66c06db4
@ -5,20 +5,22 @@ import * as vscode from 'vscode'
|
||||
import {Extension} from './main'
|
||||
import {Citation} from './providers/citation'
|
||||
import {Command} from './providers/command'
|
||||
import {Environment} from './providers/environment'
|
||||
import {Reference} from './providers/reference'
|
||||
|
||||
export class Completer implements vscode.CompletionItemProvider {
|
||||
extension: Extension
|
||||
citation: Citation
|
||||
command: Command
|
||||
reference: Citation
|
||||
environment: Citation
|
||||
environment: Environment
|
||||
reference: Reference
|
||||
|
||||
constructor(extension: Extension) {
|
||||
this.extension = extension
|
||||
this.citation = new Citation(extension)
|
||||
this.command = new Command(extension)
|
||||
this.reference = new Citation(extension)
|
||||
this.environment = new Citation(extension)
|
||||
this.environment = new Environment(extension)
|
||||
this.reference = new Reference(extension)
|
||||
}
|
||||
|
||||
provideCompletionItems(document: vscode.TextDocument, position: vscode.Position, token: vscode.CancellationToken):
|
||||
|
146
src/providers/environment.ts
Normal file
146
src/providers/environment.ts
Normal file
@ -0,0 +1,146 @@
|
||||
'use strict'
|
||||
|
||||
import * as vscode from 'vscode'
|
||||
import * as fs from 'fs'
|
||||
|
||||
import {Extension} from './../main'
|
||||
|
||||
export class Environment {
|
||||
extension: Extension
|
||||
suggestions: vscode.CompletionItem[]
|
||||
provideRefreshTime: number
|
||||
|
||||
constructor(extension: Extension) {
|
||||
this.extension = extension
|
||||
this.suggestions = []
|
||||
Object.keys(this.defaults).map(key => {
|
||||
let item = this.defaults[key]
|
||||
let environment = new vscode.CompletionItem(item.text,vscode.CompletionItemKind.Module)
|
||||
this.suggestions.push(environment)
|
||||
})
|
||||
}
|
||||
|
||||
provide() : vscode.CompletionItem[] {
|
||||
return this.suggestions
|
||||
}
|
||||
|
||||
defaults = {
|
||||
figure: {
|
||||
text: 'figure'
|
||||
},
|
||||
table: {
|
||||
text: 'table'
|
||||
},
|
||||
description: {
|
||||
text: 'description'
|
||||
},
|
||||
enumerate: {
|
||||
text: 'enumerate'
|
||||
},
|
||||
itemize: {
|
||||
text: 'itemize'
|
||||
},
|
||||
math: {
|
||||
text: 'math'
|
||||
},
|
||||
displaymath: {
|
||||
text: 'displaymath'
|
||||
},
|
||||
split: {
|
||||
text: 'split'
|
||||
},
|
||||
array: {
|
||||
text: 'array'
|
||||
},
|
||||
eqnarray: {
|
||||
text: 'eqnarray'
|
||||
},
|
||||
equation: {
|
||||
text: 'equation'
|
||||
},
|
||||
equationAst: {
|
||||
text: 'equation*'
|
||||
},
|
||||
subequations: {
|
||||
text: 'subequations'
|
||||
},
|
||||
subequationsAst: {
|
||||
text: 'subequations*'
|
||||
},
|
||||
multiline: {
|
||||
text: 'multiline'
|
||||
},
|
||||
multilineAst: {
|
||||
text: 'multiline*'
|
||||
},
|
||||
gather: {
|
||||
text: 'gather'
|
||||
},
|
||||
gatherAst: {
|
||||
text: 'gather*'
|
||||
},
|
||||
align: {
|
||||
text: 'align'
|
||||
},
|
||||
alignAst: {
|
||||
text: 'align*'
|
||||
},
|
||||
alignat: {
|
||||
text: 'alignat'
|
||||
},
|
||||
alignatAst: {
|
||||
text: 'alignat*'
|
||||
},
|
||||
flalign: {
|
||||
text: 'flalign'
|
||||
},
|
||||
flalignAst: {
|
||||
text: 'flalign*'
|
||||
},
|
||||
theorem: {
|
||||
text: 'theorem'
|
||||
},
|
||||
cases: {
|
||||
text: 'cases'
|
||||
},
|
||||
center: {
|
||||
text: 'center'
|
||||
},
|
||||
flushleft: {
|
||||
text: 'flushleft'
|
||||
},
|
||||
flushright: {
|
||||
text: 'flushright'
|
||||
},
|
||||
minipage: {
|
||||
text: 'minipage'
|
||||
},
|
||||
quotation: {
|
||||
text: 'quotation'
|
||||
},
|
||||
quote: {
|
||||
text: 'quote'
|
||||
},
|
||||
verbatim: {
|
||||
text: 'verbatim'
|
||||
},
|
||||
verse: {
|
||||
text: 'verse'
|
||||
},
|
||||
picture: {
|
||||
text: 'picture'
|
||||
},
|
||||
tabbing: {
|
||||
text: 'tabbing'
|
||||
},
|
||||
tabular: {
|
||||
text: 'tabular'
|
||||
},
|
||||
thebibliography: {
|
||||
text: 'thebibliography'
|
||||
},
|
||||
titlepage: {
|
||||
text: 'titlepage'
|
||||
}
|
||||
}
|
||||
}
|
66
src/providers/reference.ts
Normal file
66
src/providers/reference.ts
Normal file
@ -0,0 +1,66 @@
|
||||
'use strict'
|
||||
|
||||
import * as vscode from 'vscode'
|
||||
import * as fs from 'fs'
|
||||
|
||||
import {Extension} from './../main'
|
||||
|
||||
export class Reference {
|
||||
extension: Extension
|
||||
suggestions: vscode.CompletionItem[]
|
||||
provideRefreshTime: number
|
||||
|
||||
constructor(extension: Extension) {
|
||||
this.extension = extension
|
||||
}
|
||||
|
||||
provide() : vscode.CompletionItem[] {
|
||||
if (Date.now() - this.provideRefreshTime < 1000)
|
||||
return this.suggestions
|
||||
this.provideRefreshTime = Date.now()
|
||||
this.extension.manager.findAllDependentFiles()
|
||||
let suggestions = {}
|
||||
this.extension.manager.texFiles.map(filePath => {
|
||||
let items = this.getReferencesTeX(filePath)
|
||||
Object.keys(items).map(key => {
|
||||
if (!(key in suggestions))
|
||||
suggestions[key] = items[key]
|
||||
})
|
||||
})
|
||||
let items = this.getReferenceItems(vscode.window.activeTextEditor.document.getText())
|
||||
Object.keys(items).map(key => {
|
||||
if (!(key in suggestions))
|
||||
suggestions[key] = items[key]
|
||||
})
|
||||
this.suggestions = []
|
||||
Object.keys(suggestions).map(key => {
|
||||
let item = suggestions[key]
|
||||
let command = new vscode.CompletionItem(item.reference,vscode.CompletionItemKind.Reference)
|
||||
this.suggestions.push(command)
|
||||
})
|
||||
return this.suggestions
|
||||
}
|
||||
|
||||
getReferencesTeX(filePath: string) {
|
||||
if (!(fs.existsSync(filePath)))
|
||||
return {}
|
||||
return this.getReferenceItems(fs.readFileSync(filePath, 'utf-8'))
|
||||
}
|
||||
|
||||
getReferenceItems(content: string) {
|
||||
var itemReg = /(?:\\label(?:\[[^\[\]\{\}]*\])?){([^}]*)}/g
|
||||
var items = {}
|
||||
while (true) {
|
||||
let result = itemReg.exec(content);
|
||||
if (result == null) {
|
||||
break
|
||||
}
|
||||
if (!(result[1] in items)) {
|
||||
items[result[1]] = {
|
||||
reference: result[1]
|
||||
}
|
||||
}
|
||||
}
|
||||
return items
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user