Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onsaverunner feature #58

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ This project adhere's to [Semantic Versioning](https://semver.org/spec/v2.0.0.ht

## [unreleased]

- Add `onSaveCommand` to `LanguageConfig`. Can now configure a command to run on saved files.
- Restarting Alloglot no longer disposes of its main output channel.

## [3.0.4]

- Fix crashing `Alloglot: Restart` command.
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,26 @@ export type LanguageConfig = {
languageId: string

/**
* A command to start the language server.
* A shell command to start the language server.
*/
serverCommand?: string

/**
* A formatter command.
* Reads from STDIN and writes to STDOUT.
* `${file}` will be replaced with the relative path to the file.
* A formatter shell command.
* STDIN will be equal to the contents of the current text document,
* not the file contents as it exists on disk.
* STDOUT will replace the entire contents of the current text document.
* Alloglot will not modify the file on disk (though your command might!).
* `${file}` will be replaced with the full path to the file.
*/
formatCommand?: string

/**
* A shell command to run after a file is saved.
* `${file}` will be replaced with the full path to the file.
*/
onSaveCommand?: string

/**
* URL to documentation/API search.
* `${query}` will be replaced with the symbol under cursor.
Expand Down Expand Up @@ -247,7 +256,7 @@ export type TagsConfig = {

/**
* A command to refresh the tags file when a file is saved.
* `${file}` will be replaced with the relative path to the file.
* `${file}` will be replaced with the full path to the file.
*/
refreshTagsCommand?: string

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@
"formatCommand": {
"type": "string"
},
"onSaveCommand": {
"type": "string"
},
"apiSearchUrl": {
"type": "string"
},
Expand Down
27 changes: 21 additions & 6 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,26 @@ export type LanguageConfig = {
languageId: string

/**
* A command to start the language server.
* A shell command to start the language server.
*/
serverCommand?: string

/**
* A formatter command.
* Reads from STDIN and writes to STDOUT.
* `${file}` will be replaced with the relative path to the file.
* A formatter shell command.
* STDIN will be equal to the contents of the current text document,
* not the file contents as it exists on disk.
* STDOUT will replace the entire contents of the current text document.
* Alloglot will not modify the file on disk (though your command might!).
* `${file}` will be replaced with the full path to the file.
*/
formatCommand?: string

/**
* A shell command to run after a file is saved.
* `${file}` will be replaced with the full path to the file.
*/
onSaveCommand?: string

/**
* URL to documentation/API search.
* `${query}` will be replaced with the symbol under cursor.
Expand Down Expand Up @@ -90,7 +99,7 @@ export type TagsConfig = {

/**
* A command to refresh the tags file when a file is saved.
* `${file}` will be replaced with the relative path to the file.
* `${file}` will be replaced with the full path to the file.
*/
refreshTagsCommand?: string

Expand Down Expand Up @@ -235,7 +244,7 @@ export namespace Config {
const languages = workspaceSettings.get<Array<LanguageConfig>>(alloglot.config.languages)
const verboseOutput = workspaceSettings.get<boolean>(alloglot.config.verboseOutput)
const mergeConfigs = workspaceSettings.get<boolean>(alloglot.config.mergeConfigs)
const grepPath = workspaceSettings.get<string>('grepPath')
const grepPath = workspaceSettings.get<string>(alloglot.config.grepPath)
const settingsExist = !!activateCommand || !!languages || !!deactivateCommand || !!verboseOutput || !!mergeConfigs || !!grepPath
output.appendLine(alloglot.ui.workspaceConfigExists(settingsExist))
if (settingsExist) return { activateCommand, deactivateCommand, languages, verboseOutput, mergeConfigs, grepPath }
Expand All @@ -258,6 +267,7 @@ export namespace Config {
lang.languageId = lang.languageId.trim()
lang.serverCommand = lang.serverCommand?.trim()
lang.formatCommand = lang.formatCommand?.trim()
lang.onSaveCommand = lang.onSaveCommand?.trim()
lang.apiSearchUrl = lang.apiSearchUrl?.trim()

lang.annotations = lang.annotations?.filter(ann => {
Expand Down Expand Up @@ -385,9 +395,11 @@ export namespace alloglot {
export const registeredCompletionsProvider = 'Registered completions provider.'
export const registeredDefinitionsProvider = 'Registered definitions provider.'
export const registeredImportsProvider = 'Registered imports provider.'
export const registeredOnSaveCommand = 'Registered on-save command.'
export const registeringCompletionsProvider = 'Registering completions provider...'
export const registeringDefinitionsProvider = 'Registering definitions provider...'
export const registeringImportsProvider = 'Registering imports provider...'
export const registeringOnSaveCommand = 'Registering on-save command...'
export const renderedImportLine = (line?: string) => `Rendered import line: ${line}`
export const renderedModuleName = (name?: string) => `Rendered module name: ${name}`
export const renderingImportLine = (tag: any) => `Rendering import line for tag: ${JSON.stringify(tag)}`
Expand Down Expand Up @@ -420,6 +432,7 @@ export namespace alloglot {
export const annotations = 'annotations' as const
export const formatter = 'formatter' as const
export const client = 'client' as const
export const onSaveRunner = 'onsaverunner' as const
export const tags = 'tags' as const
export const tagsSource = 'tagssource' as const
export const importsProvider = 'importsprovider' as const
Expand All @@ -435,8 +448,10 @@ export namespace alloglot {
export namespace config {
export const root = alloglot.root
export const fallbackPath = `.vscode/${root}.json` as const
export const grepPath = 'grepPath' as const
export const languages = 'languages' as const
export const activateCommand = 'activateCommand' as const
export const onSaveCommand = 'onSaveCommand' as const
export const deactivateCommand = 'deactivateCommand' as const
export const verboseOutput = 'verboseOutput' as const
export const mergeConfigs = 'mergeConfigs' as const
Expand Down
11 changes: 4 additions & 7 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,17 @@ import { makeApiSearch } from './apisearch'
import { makeClient } from './client'
import { Config, TConfig, alloglot } from './config'
import { makeFormatter } from './formatter'
import { makeOnSaveRunner } from './onsaverunner'
import { makeTags } from './tags'
import { AsyncProcess, HierarchicalOutputChannel, IHierarchicalOutputChannel } from './utils'

let globalOutput: vscode.OutputChannel | undefined
let globalOutput: IHierarchicalOutputChannel | undefined
let globalContext: vscode.ExtensionContext | undefined
let globalConfig: TConfig | undefined

export function activate(context: vscode.ExtensionContext): void {
if (globalOutput) {
globalOutput.dispose()
globalOutput = undefined
}

globalContext = context
const output = HierarchicalOutputChannel.make(alloglot.root)
const output = globalOutput || HierarchicalOutputChannel.make(alloglot.root)
globalOutput = output

output.appendLine(alloglot.ui.startingAlloglot)
Expand All @@ -46,6 +42,7 @@ export function activate(context: vscode.ExtensionContext): void {
makeApiSearch(output.local(alloglot.components.apiSearch), config),

// Start all the language-specific components.
...langs.map(lang => makeOnSaveRunner(output.local(alloglot.components.onSaveRunner).local(lang.languageId), lang)),
...langs.map(lang => makeAnnotations(output.local(alloglot.components.annotations).local(lang.languageId), lang)),
...langs.map(lang => makeFormatter(output.local(alloglot.components.formatter).local(lang.languageId), lang, verboseOutput)),
...langs.map(lang => makeClient(output.local(alloglot.components.client).local(lang.languageId), lang)),
Expand Down
33 changes: 33 additions & 0 deletions src/onsaverunner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as vscode from 'vscode'

import { LanguageConfig, alloglot } from './config'
import { AsyncProcess, Disposal } from './utils'

export function makeOnSaveRunner(output: vscode.OutputChannel, config: LanguageConfig): vscode.Disposable {
const { languageId, onSaveCommand } = config
if (!languageId || !onSaveCommand) return vscode.Disposable.from()

const disposal = Disposal.make()
const basedir = vscode.workspace.workspaceFolders?.[0].uri
output.appendLine(alloglot.ui.registeringOnSaveCommand)

const onSaveWatcher = (() => {
if (!onSaveCommand) return vscode.Disposable.from()

const refreshTags = (doc: vscode.TextDocument) => {
if (doc.languageId === languageId) {
const command = onSaveCommand.replace('${file}', doc.fileName)
disposal.insert(AsyncProcess.make({ output, command, basedir }, () => undefined))
}
}

return vscode.workspace.onDidSaveTextDocument(refreshTags)
})()

output.appendLine(alloglot.ui.registeredOnSaveCommand)

return vscode.Disposable.from(
disposal,
onSaveWatcher
)
}
Loading