support multi root workspace

This commit is contained in:
innerlee 2017-11-16 01:39:55 +08:00
parent d4745fcd16
commit 8851da05da
4 changed files with 4124 additions and 10 deletions

4082
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -12,7 +12,7 @@
"url": "https://github.com/James-Yu/LaTeX-Workshop.git"
},
"engines": {
"vscode": "^1.13.0"
"vscode": "^1.17.2"
},
"categories": [
"Languages",
@ -453,7 +453,7 @@
"@types/ws": "^0.0.39",
"tslint": "^5.0.0",
"typescript": "^2.0.3",
"vscode": "^1.0.0",
"vscode": "^1.1.6",
"webpack": "^2.7.0"
}
}

View File

@ -7,7 +7,8 @@ import {Extension} from '../main'
export class Manager {
extension: Extension
rootFile: string
rootFiles: object
workspace: string
texFileTree: { [id: string]: Set<string> } = {}
fileWatcher: chokidar.FSWatcher
bibWatcher: chokidar.FSWatcher
@ -15,13 +16,23 @@ export class Manager {
constructor(extension: Extension) {
this.extension = extension
this.watched = []
this.watched = []
this.rootFiles = {}
this.workspace = ''
}
get rootDir() {
return path.dirname(this.rootFile)
}
get rootFile() {
return this.rootFiles[this.workspace]
}
set rootFile(root: string) {
this.rootFiles[this.workspace] = root
}
tex2pdf(texPath: string) {
const configuration = vscode.workspace.getConfiguration('latex-workshop')
const outputDir = configuration.get('latex.outputDir') as string
@ -32,7 +43,24 @@ export class Manager {
return ['.tex', '.sty', '.cls', '.bbx', '.cbx'].indexOf(path.extname(filePath)) > -1
}
updateWorkspace() {
let wsroot = vscode.workspace.rootPath
if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) {
wsroot = vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri).uri.fsPath
}
if (wsroot) {
if (wsroot !== this.workspace) {
this.workspace = wsroot
this.extension.nodeProvider.refresh()
this.extension.nodeProvider.update()
}
} else {
this.workspace = ''
}
}
findRoot() : string | undefined {
this.updateWorkspace()
const findMethods = [() => this.findRootMagic(), () => this.findRootSelf(), () => this.findRootSaved(), () => this.findRootDir()]
for (const method of findMethods) {
const rootFile = method()
@ -88,22 +116,22 @@ export class Manager {
findRootDir() : string | undefined {
const regex = /\\begin{document}/m
if (!vscode.workspace.rootPath) {
if (!this.workspace) {
return undefined
}
try {
const files = fs.readdirSync(vscode.workspace.rootPath)
const files = fs.readdirSync(this.workspace)
for (let file of files) {
if (path.extname(file) !== '.tex') {
continue
}
file = path.join(vscode.workspace.rootPath, file)
file = path.join(this.workspace, file)
const content = fs.readFileSync(file)
const result = content.toString().match(regex)
if (result) {
file = path.resolve(vscode.workspace.rootPath, file)
file = path.resolve(this.workspace, file)
this.extension.logger.addLogMessage(`Found root file in root directory: ${file}`)
return file
}

View File

@ -26,8 +26,12 @@ export class SectionNodeProvider implements vscode.TreeDataProvider<Section> {
}
refresh() : Section[] {
this.ds = this.buildModel(this.extension.manager.rootFile)
return this.ds
if (this.extension.manager.rootFile) {
this.ds = this.buildModel(this.extension.manager.rootFile)
return this.ds
} else {
return []
}
}
update() {