Add support to single file counter and float counts

This commit is contained in:
James-Yu 2017-08-02 14:39:09 +08:00
parent 5de6c6267a
commit e90d7b2148
5 changed files with 25 additions and 13 deletions

View File

@ -1,8 +1,9 @@
# Change Log
## [3.2.0] - Unreleased
## [3.2.0] - 2017-08-02
### Added
- (#219) Show all symbol definitions within a document and the project.
- (#224) Count single file words and floats when not opening the root LaTeX file.
## [3.1.2] - 2017-07-31
### Fixed

View File

@ -334,10 +334,8 @@
},
"latex-workshop.texcount.args": {
"type": "array",
"default": [
"-merge"
],
"description": "TeXCount arguments to count words in LaTeX document of the entire project from the root file.\nArguments must be in separate strings in the array. Additional arguments, i.e., `%DOC%` will be appended when constructing the command."
"default": [],
"description": "TeXCount arguments to count words in LaTeX document of the entire project from the root file, or the currect document.\nArguments must be in separate strings in the array. Additional arguments, i.e., `-merge %DOC%` for the project and the current document path for counting current file will be appended when constructing the command."
},
"latex-workshop.intellisense.citation.type": {
"type": "string",

View File

@ -86,7 +86,12 @@ export class Commander {
wordcount() {
this.extension.logger.addLogMessage(`WORDCOUNT command invoked.`)
this.extension.counter.count()
if (!vscode.window.activeTextEditor || !this.extension.manager.isTex(vscode.window.activeTextEditor.document.fileName) ||
this.extension.manager.rootFile === vscode.window.activeTextEditor.document.fileName) {
this.extension.counter.count(this.extension.manager.rootFile)
} else {
this.extension.counter.count(vscode.window.activeTextEditor.document.fileName, false)
}
}
log() {

View File

@ -1,4 +1,5 @@
import * as vscode from 'vscode'
import * as path from 'path'
import * as cp from 'child_process'
import {Extension} from '../main'
@ -10,13 +11,16 @@ export class Counter {
this.extension = extension
}
count() {
count(file: string, merge: boolean = true) {
if (this.extension.manager.rootFile !== undefined) {
this.extension.manager.findRoot()
}
const configuration = vscode.workspace.getConfiguration('latex-workshop')
const args = configuration.get('texcount.args') as string[]
const proc = cp.spawn(configuration.get('texcount.path') as string, args.concat([this.extension.manager.rootFile]))
if (merge) {
args.push('-merge')
}
const proc = cp.spawn(configuration.get('texcount.path') as string, args.concat([file]), {cwd: path.dirname(file)})
proc.stdout.setEncoding('utf8')
proc.stderr.setEncoding('utf8')
@ -40,9 +44,14 @@ export class Counter {
this.extension.logger.addLogMessage(`Cannot count words, code: ${exitCode}, ${stderr}`)
vscode.window.showErrorMessage('TeXCount failed. Please refer to LaTeX Workshop Output for details.')
} else {
const result = /Words in text: ([0-9]*)/g.exec(stdout)
if (result) {
vscode.window.showInformationMessage(`There are ${result[1]} words in the current LaTeX project. Please refer to LaTeX Workshop Output for details.`)
const words = /Words in text: ([0-9]*)/g.exec(stdout)
const floats = /Number of floats\/tables\/figures: ([0-9]*)/g.exec(stdout)
if (words) {
let floatMsg = ''
if (floats && parseInt(floats[1]) > 0) {
floatMsg = `and ${floats[1]} float${parseInt(floats[1]) > 1 ? 's' : ''} (tables, figures, etc.) `
}
vscode.window.showInformationMessage(`There are ${words[1]} words ${floatMsg}in the ${merge ? 'LaTeX project' : 'opened LaTeX file'}.`)
}
this.extension.logger.addLogMessage(`TeXCount log:\n${stdout}`)
}

View File

@ -52,8 +52,7 @@ export class SectionNodeProvider implements vscode.TreeDataProvider<Section> {
return rootStack.length === 0
}
this.extension.logger.addLogMessage(`Parsing ${filePath}`)
//console.log(`Parsing ${filePath}`)
this.extension.logger.addLogMessage(`Parsing ${filePath} for outline`)
const content = fs.readFileSync(filePath, 'utf-8')
let pattern = "^((?:\\\\(?:input|include|subfile)(?:\\[[^\\[\\]\\{\\}]*\\])?){([^}]*)})|^((?:\\\\("