mirror of
https://github.com/James-Yu/LaTeX-Workshop.git
synced 2024-10-04 23:27:20 +03:00
moved addroot functionality into texmagician component
This commit is contained in:
parent
589b1d5166
commit
eb9550629d
@ -214,7 +214,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"command": "latex-workshop.addroot",
|
"command": "latex-workshop.addroot",
|
||||||
"title": "Insert root document",
|
"title": "Insert %!TeX root magic command",
|
||||||
"category": "LaTeX Workshop"
|
"category": "LaTeX Workshop"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -2,7 +2,6 @@ import * as vscode from 'vscode'
|
|||||||
import * as opn from 'opn'
|
import * as opn from 'opn'
|
||||||
|
|
||||||
import {Extension} from './main'
|
import {Extension} from './main'
|
||||||
import {EOL} from 'os'
|
|
||||||
|
|
||||||
export class Commander {
|
export class Commander {
|
||||||
extension: Extension
|
extension: Extension
|
||||||
@ -125,51 +124,9 @@ export class Commander {
|
|||||||
return this.extension.cleaner.clean()
|
return this.extension.cleaner.clean()
|
||||||
}
|
}
|
||||||
|
|
||||||
getFileName(file: string) : string {
|
|
||||||
return file.replace(/\\/g, '/').match(/([^\/]+$)/)[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
getRelativePath(file: string, currentFile: string) : string {
|
|
||||||
// replace '\' in windows paths with '/'
|
|
||||||
file = file.replace(/\\/g, '/')
|
|
||||||
// get path of current folder, including to the last '/'
|
|
||||||
let currentFolder = currentFile.replace(/\\/g, '/').replace(/[^\/]+$/gi, '')
|
|
||||||
// find index up to which paths match
|
|
||||||
let i = 0
|
|
||||||
while ( file.charAt(i) === currentFolder.charAt(i)) {
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
// select nonmatching substring
|
|
||||||
file = file.substring(i)
|
|
||||||
currentFolder = currentFolder.substring(i)
|
|
||||||
// replace each '/foldername/' in path with '/../'
|
|
||||||
currentFolder = currentFolder.replace(/[^/]+/g, '..')
|
|
||||||
return './' + currentFolder + file
|
|
||||||
}
|
|
||||||
|
|
||||||
addroot() {
|
addroot() {
|
||||||
this.extension.logger.addLogMessage(`ADDROOT command invoked.`)
|
this.extension.logger.addLogMessage(`ADDROOT command invoked.`)
|
||||||
if (vscode.window.activeTextEditor === null) {
|
this.extension.texMagician.addroot()
|
||||||
console.log('File must be opened')
|
|
||||||
}
|
|
||||||
// taken from here: https://github.com/DonJayamanne/listFilesVSCode/blob/master/src/extension.ts (MIT licensed, should be fine)
|
|
||||||
vscode.workspace.findFiles('**/*.{tex}').then(files => {
|
|
||||||
const displayFiles = files.map(file => {
|
|
||||||
return { description: file.fsPath, label: this.getFileName(file.fsPath), filePath: file.fsPath }
|
|
||||||
})
|
|
||||||
vscode.window.showQuickPick(displayFiles).then(val => {
|
|
||||||
const editor = vscode.window.activeTextEditor
|
|
||||||
if (val != null && editor != null) {
|
|
||||||
const relativePath = this.getRelativePath(val.filePath, editor.document.fileName)
|
|
||||||
const edits = [vscode.TextEdit.insert(new vscode.Position(0, 0), `% !TeX root = ${relativePath}${EOL}`)]
|
|
||||||
// Insert the text
|
|
||||||
const uri = editor.document.uri
|
|
||||||
const edit = new vscode.WorkspaceEdit()
|
|
||||||
edit.set(uri, edits)
|
|
||||||
vscode.workspace.applyEdit(edit)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
59
src/components/texmagician.ts
Normal file
59
src/components/texmagician.ts
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
import * as vscode from 'vscode'
|
||||||
|
import {EOL} from 'os'
|
||||||
|
import {Extension} from '../main'
|
||||||
|
|
||||||
|
export class TeXMagician {
|
||||||
|
extension: Extension
|
||||||
|
|
||||||
|
constructor(extension: Extension) {
|
||||||
|
this.extension = extension
|
||||||
|
}
|
||||||
|
|
||||||
|
getFileName(file: string) : string {
|
||||||
|
return file.replace(/\\/g, '/').match(/([^\/]+$)/)[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
getRelativePath(file: string, currentFile: string) : string {
|
||||||
|
// replace '\' in windows paths with '/'
|
||||||
|
file = file.replace(/\\/g, '/')
|
||||||
|
// get path of current folder, including to the last '/'
|
||||||
|
let currentFolder = currentFile.replace(/\\/g, '/').replace(/[^\/]+$/gi, '')
|
||||||
|
// find index up to which paths match
|
||||||
|
let i = 0
|
||||||
|
while ( file.charAt(i) === currentFolder.charAt(i)) {
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
// select nonmatching substring
|
||||||
|
file = file.substring(i)
|
||||||
|
currentFolder = currentFolder.substring(i)
|
||||||
|
// replace each '/foldername/' in path with '/../'
|
||||||
|
currentFolder = currentFolder.replace(/[^/]+/g, '..')
|
||||||
|
return './' + currentFolder + file
|
||||||
|
}
|
||||||
|
|
||||||
|
addroot() {
|
||||||
|
if (vscode.window.activeTextEditor === null) {
|
||||||
|
console.log('File must be opened')
|
||||||
|
}
|
||||||
|
// taken from here: https://github.com/DonJayamanne/listFilesVSCode/blob/master/src/extension.ts (MIT licensed, should be fine)
|
||||||
|
vscode.workspace.findFiles('**/*.{tex}').then(files => {
|
||||||
|
const displayFiles = files.map(file => {
|
||||||
|
return { description: file.fsPath, label: this.getFileName(file.fsPath), filePath: file.fsPath }
|
||||||
|
})
|
||||||
|
vscode.window.showQuickPick(displayFiles).then(val => {
|
||||||
|
const editor = vscode.window.activeTextEditor
|
||||||
|
if (val != null && editor != null) {
|
||||||
|
const relativePath = this.getRelativePath(val.filePath, editor.document.fileName)
|
||||||
|
const edits = [vscode.TextEdit.insert(new vscode.Position(0, 0), `% !TeX root = ${relativePath}${EOL}`)]
|
||||||
|
// Insert the text
|
||||||
|
const uri = editor.document.uri
|
||||||
|
const edit = new vscode.WorkspaceEdit()
|
||||||
|
edit.set(uri, edits)
|
||||||
|
vscode.workspace.applyEdit(edit)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -14,6 +14,7 @@ import {Parser} from './components/parser'
|
|||||||
import {Linter} from './components/linter'
|
import {Linter} from './components/linter'
|
||||||
import {Cleaner} from './components/cleaner'
|
import {Cleaner} from './components/cleaner'
|
||||||
import {Counter} from './components/counter'
|
import {Counter} from './components/counter'
|
||||||
|
import {TeXMagician} from './components/texmagician'
|
||||||
|
|
||||||
import {Completer} from './providers/completion'
|
import {Completer} from './providers/completion'
|
||||||
import {CodeActions} from './providers/codeactions'
|
import {CodeActions} from './providers/codeactions'
|
||||||
@ -230,6 +231,7 @@ export class Extension {
|
|||||||
counter: Counter
|
counter: Counter
|
||||||
codeActions: CodeActions
|
codeActions: CodeActions
|
||||||
nodeProvider: SectionNodeProvider
|
nodeProvider: SectionNodeProvider
|
||||||
|
texMagician: TeXMagician
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.extensionRoot = path.resolve(`${__dirname}/../../`)
|
this.extensionRoot = path.resolve(`${__dirname}/../../`)
|
||||||
@ -247,6 +249,7 @@ export class Extension {
|
|||||||
this.counter = new Counter(this)
|
this.counter = new Counter(this)
|
||||||
this.codeActions = new CodeActions(this)
|
this.codeActions = new CodeActions(this)
|
||||||
this.nodeProvider = new SectionNodeProvider(this)
|
this.nodeProvider = new SectionNodeProvider(this)
|
||||||
|
this.texMagician = new TeXMagician(this)
|
||||||
|
|
||||||
this.logger.addLogMessage(`LaTeX Workshop initialized.`)
|
this.logger.addLogMessage(`LaTeX Workshop initialized.`)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user