Added IncludeManager, IncludeHolder and DescriptorHolder classes for dealing with merging files at include points and keeping track of files to not open files continuously [CI-SKIP]

This commit is contained in:
Noah Santschi-Cooney 2018-05-29 00:06:55 +01:00
parent ff35b62142
commit a27da8584f
No known key found for this signature in database
GPG key ID: 3B22282472C8AE48
4 changed files with 89 additions and 13 deletions

View file

@ -1,21 +1,19 @@
'use strict';
import * as vscode from 'vscode'
import GLSLProvider from './linter/glslProvider'
import * as shell from 'shelljs'
let glslProv: GLSLProvider;
export let glslProv: GLSLProvider;
export function activate(context: vscode.ExtensionContext) {
glslProv = new GLSLProvider(context.subscriptions)
vscode.languages.registerCodeActionsProvider('glsl', glslProv)
glslProv = new GLSLProvider(context.subscriptions)
vscode.languages.registerCodeActionsProvider('glsl', glslProv)
}
export function deactivate() {
try {
console.log('[MC-GLSL] disposing')
shell.rm('-rf', glslProv.getConfig().tmpdir)
} catch (e) {
console.log(e)
}
try {
console.log('[MC-GLSL] disposing')
shell.rm('-rf', glslProv.getConfig().tmpdir)
} catch (e) {
console.log(e)
}
}

22
src/fileDescriptors.ts Normal file
View file

@ -0,0 +1,22 @@
import * as fs from 'fs'
import * as vscode from 'vscode'
import GLSLProvider from './linter/glslProvider'
export class DescriptorHolder {
private holder: {[path: string]: number} = {}
public add(path: vscode.Uri) {
fs.open(GLSLProvider.getTempFilePath(path.path), 'r', (err, fd) => {
this.holder[path.path] = fd
})
}
public clear = () => {
for (const path in this.holder) {
if (this.holder.hasOwnProperty(path)) {
fs.close(this.holder[path])
delete this.holder[path]
}
}
}
}

View file

@ -1,5 +1,3 @@
'use strict'
import * as vscode from 'vscode'
import * as cp from 'child_process'
import * as fs from 'fs'
@ -7,6 +5,8 @@ import * as shell from 'shelljs'
import * as path from 'path'
import '../global'
import { Config } from '../config'
import { glslProv } from '../extension'
import { IncludeHolder, IncludeManager } from './includeManager'
// These are used for symlinking as glslangValidator only accepts files in these formats
const extensions: { [id: string]: string } = {
@ -36,6 +36,7 @@ export default class GLSLProvider implements vscode.CodeActionProvider {
private diagnosticCollection: vscode.DiagnosticCollection // where errors/warnings/hints are pushed to be displayed
private config: Config
private onTypeDisposable?: vscode.Disposable
private includesHolder: IncludeHolder
constructor(subs: vscode.Disposable[]) {
this.diagnosticCollection = vscode.languages.createDiagnosticCollection()
@ -43,6 +44,7 @@ export default class GLSLProvider implements vscode.CodeActionProvider {
subs.push(this)
this.config = new Config()
this.includesHolder = new IncludeHolder()
const c = vscode.workspace.getConfiguration('mcglsl')
if (c.get('lintOnType') as boolean) {
@ -85,6 +87,8 @@ export default class GLSLProvider implements vscode.CodeActionProvider {
public getConfig = () => this.config
public static getTempFilePath = (s: string) => path.join(glslProv.getConfig().tmpdir, `${path.basename(s, path.extname(s))}${extensions[path.extname(s)]}`)
public dispose = () => this.diagnosticCollection.dispose()
// Maybe only lint when files are saved...hmmm
@ -111,6 +115,9 @@ export default class GLSLProvider implements vscode.CodeActionProvider {
private lint(document: vscode.TextDocument) {
if (document.languageId !== 'glsl') return
this.includesHolder.add(document.uri)
this.includesHolder.get(document.uri).push()
const linkname = path.join(this.config.tmpdir, `${path.basename(document.fileName, path.extname(document.fileName))}${extensions[path.extname(document.fileName)]}`)
this.createSymlinks(linkname, document)

View file

@ -0,0 +1,49 @@
import * as vscode from 'vscode'
import * as fs from 'fs'
import GLSLProvider from './glslProvider';
export class IncludeManager {
private readonly queue: null[] = []
private readonly path: string;
constructor(path: string) {
this.path = path
}
public push() {
this.queue.push(null)
if (this.queue.length === 1) {
this.mergeFiles()
}
}
private async mergeFiles() {
while (this.queue.length > 0) {
// WIP
/* this.queue.pop()
let text = ''
console.log('yes')
await fs.readFile(GLSLProvider.getTempFilePath(this.path), (err: NodeJS.ErrnoException, data: Buffer) => {
if (err) {
resolve(err)
console.log(err)
return
}
text += data
})
console.log(text) */
}
}
}
export class IncludeHolder {
public managers: { [file: string]: IncludeManager } = {}
public add(file: vscode.Uri) {
if (!this.managers.hasOwnProperty(file.path)) {
this.managers[file.path] = new IncludeManager(file.path)
}
}
public get = (file: vscode.Uri) => this.managers[file.path]
}