Skip to content

Commit

Permalink
adds custom file association support to filesystem event watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Strum355 committed Feb 19, 2021
1 parent c854093 commit 248afcd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 12 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

## Unreleased

### Added

- Filesystem watcher reads custom defined file associations

### Fixed

- Fixed `#include` merging for when file is merged twice that would normally be `#ifdef` guarded. Please see commit message of [551380a](https://github.com/Strum355/mcshader-lsp/commit/551380a6ed00709287460b7d8c88e7803956052c) for detailed explanation.
- Fixed `#include` merging for when file is merged twice that would normally be `#ifdef` guarded. Please see commit message of [551380a](https://github.com/Strum355/mcshader-lsp/commit/551380a6ed00709287460b7d8c88e7803956052c) for detailed explanation

## [0.9.4]

Expand Down
32 changes: 29 additions & 3 deletions client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,38 @@ export class Extension {
this.registerCommand('virtualMerge', commands.virtualMergedDocument)

log.info('starting language server...')

const lspBinary = process.env['MCSHADER_DEBUG'] ?
this.context.asAbsolutePath(path.join('server', 'target', 'debug', 'mcshader-lsp')) +
(process.platform === 'win32' ? '.exe' : '') :
path.join(this.context.globalStoragePath, 'mcshader-lsp')

const filewatcherGlob = this.fileAssociationsToGlob(this.getGLSLFileAssociations())

this.client = await new LanguageClient(this).startServer()
this.client = await new LanguageClient(this, lspBinary, filewatcherGlob).startServer()

log.info('language server started!')
}

fileAssociationsToGlob = (associations: string[]): string => {
return '**/*.{'.concat(
associations.map(s => s.substring(s.indexOf('.'))).join(',')
) + '}'
}

getGLSLFileAssociations = (): string[] => {
const exts = ['.fsh', '.vsh', '.gsh', '.glsl']
const associations = vscode.workspace.getConfiguration('files').get('associations') as {[key: string]: string}

Object.keys(associations).forEach((key) => {
if(associations[key] === 'glsl') {
exts.push(key.substring(key.indexOf('*')+1))
}
})

return exts
}

registerCommand = (name: string, f: (e: Extension) => commands.Command) => {
const cmd = f(this)
this.context.subscriptions.push(vscode.commands.registerCommand('mcglsl.'+name, cmd))
Expand Down Expand Up @@ -83,7 +109,7 @@ export class Extension {
if (!exists) await this.state.updateServerVersion(undefined)

const release = await getReleaseInfo(this.package.version)
log.info(`got release info from Github:\n\t`, JSON.stringify(release))
log.info('got release info from Github:\n\t', JSON.stringify(release))

const platform = platforms[`${process.arch} ${process.platform}`]
if (platform === undefined) {
Expand All @@ -93,7 +119,7 @@ export class Extension {
}

if (release.tag_name === this.state.serverVersion) {
log.info(`server version is same as extension:\n\t`, this.state.serverVersion)
log.info('server version is same as extension:\n\t', this.state.serverVersion)
return
}

Expand Down
15 changes: 7 additions & 8 deletions client/src/lspClient.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import * as path from 'path'
import { ConfigurationTarget, workspace } from 'vscode'
import * as lsp from 'vscode-languageclient'
import { Extension } from './extension'
import { lspOutputChannel } from './log'
import { log, lspOutputChannel } from './log'
import { ConfigUpdateParams, statusMethod, StatusParams, updateConfigMethod } from './lspExt'

export class LanguageClient extends lsp.LanguageClient {
private extension: Extension

constructor(ext: Extension) {
constructor(ext: Extension, lspBinary: string, filewatcherGlob: string) {
super('vscode-mc-shader', 'VSCode MC Shader', {
command: process.env['MCSHADER_DEBUG'] ?
ext.context.asAbsolutePath(path.join('server', 'target', 'debug', 'mcshader-lsp')) +
(process.platform === 'win32' ? '.exe' : '') :
path.join(ext.context.globalStoragePath, 'mcshader-lsp')
command: lspBinary
}, {
documentSelector: [{scheme: 'file', language: 'glsl'}],
outputChannel: lspOutputChannel,
synchronize: {
configurationSection: 'mcglsl',
fileEvents: workspace.createFileSystemWatcher('**/*.{fsh,gsh,vsh,glsl,inc}')
fileEvents: workspace.createFileSystemWatcher(filewatcherGlob)
},
})
this.extension = ext

log.info('server receiving events for file glob:\n\t', filewatcherGlob)
log.info('running with binary at path:\n\t', lspBinary)
}

public startServer = async (): Promise<LanguageClient> => {
Expand Down

0 comments on commit 248afcd

Please sign in to comment.