-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
207 additions
and
37 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
packages/langium-vscode/src/language-server/grammar-workspace-manager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/****************************************************************************** | ||
* Copyright 2022 TypeFox GmbH | ||
* This program and the accompanying materials are made available under the | ||
* terms of the MIT License, which is available in the project root. | ||
******************************************************************************/ | ||
|
||
import ignore, { Ignore } from 'ignore'; | ||
import { CancellationToken, WorkspaceFolder } from 'vscode-languageserver-protocol'; | ||
import { URI, Utils } from 'vscode-uri'; | ||
import { LangiumSharedServices } from 'langium/src/services'; | ||
import { ConfigurationProvider } from 'langium/src/workspace/configuration'; | ||
import { FileSystemNode } from 'langium/src/workspace/file-system-provider'; | ||
import { DefaultWorkspaceManager } from 'langium/src/workspace/workspace-manager'; | ||
import { LangiumGrammarLanguageMetaData } from 'langium/src/grammar/generated/module'; | ||
|
||
const CONFIG_KEY = 'build'; | ||
|
||
interface WorkspaceManagerConf { | ||
/** | ||
* gitignore separated exclusion patterns, separated by comma | ||
*/ | ||
ignorePatterns: string | ||
} | ||
|
||
export class LangiumGrammarWorkspaceManager extends DefaultWorkspaceManager { | ||
|
||
protected readonly configurationProvider: ConfigurationProvider; | ||
protected matcher: Ignore | undefined; | ||
|
||
constructor(services: LangiumSharedServices) { | ||
super(services); | ||
this.configurationProvider = services.workspace.ConfigurationProvider; | ||
} | ||
|
||
async initializeWorkspace(folders: WorkspaceFolder[], cancelToken = CancellationToken.None): Promise<void> { | ||
const buildConf: WorkspaceManagerConf = await this.configurationProvider.getConfiguration(LangiumGrammarLanguageMetaData.languageId, CONFIG_KEY); | ||
const ignorePatterns = buildConf.ignorePatterns?.split(',')?.map(pattern => pattern.trim())?.filter(pattern => pattern.length > 0); | ||
this.matcher = ignorePatterns ? ignore().add(ignorePatterns) : undefined; | ||
return super.initializeWorkspace(folders, cancelToken); | ||
} | ||
|
||
protected includeEntry(entry: FileSystemNode, fileExtensions: string[]): boolean { | ||
if (this.matcher) { | ||
const parentWsFolder = this.folders?.map(folder => folder.uri).find(folderUri => entry.uri.toString().startsWith(folderUri)); | ||
let entryPath = entry.uri.path; | ||
if (parentWsFolder) { | ||
// create path relative to workspace folder root: /user/foo/workspace/entry.txt -> entry.txt | ||
const wsFolderPath = URI.parse(parentWsFolder).path + '/'; | ||
entryPath = entryPath.replace(wsFolderPath, ''); | ||
} | ||
const ignored = this.matcher.ignores(entryPath); | ||
return !ignored && (entry.isDirectory || (entry.isFile && fileExtensions.includes(Utils.extname(entry.uri)))); | ||
} | ||
return super.includeEntry(entry, fileExtensions); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/****************************************************************************** | ||
* Copyright 2022 TypeFox GmbH | ||
* This program and the accompanying materials are made available under the | ||
* terms of the MIT License, which is available in the project root. | ||
******************************************************************************/ | ||
|
||
import { DidChangeConfigurationParams } from 'vscode-languageserver'; | ||
import { ConfigurationItem, DidChangeConfigurationNotification } from 'vscode-languageserver-protocol'; | ||
import { LangiumSharedServices } from '../services'; | ||
|
||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
|
||
export interface ConfigurationProvider { | ||
/** | ||
* Returns a configuration value stored for the given language. | ||
* | ||
* @language the language id | ||
* @configuration configuration name | ||
*/ | ||
getConfiguration(language: string, configuration: string): Promise<any>; | ||
|
||
/** | ||
* Updates the cached configurations using the `change` notification parameters. | ||
* | ||
* @param change The parameters of a change configuration notification. | ||
* `settings` property of the change object could be expressed as `Record<string, Record<string, any>>` | ||
*/ | ||
updateConfiguration(change: DidChangeConfigurationParams): void; | ||
} | ||
|
||
export class DefaultConfigurationProvider implements ConfigurationProvider { | ||
|
||
protected settings: Record<string, Record<string, any>> = {}; | ||
protected workspaceConfig = false; | ||
protected initialized = false; | ||
protected readonly sharedServices: LangiumSharedServices; | ||
|
||
constructor(services: LangiumSharedServices) { | ||
this.sharedServices = services; | ||
services.lsp.LanguageServer.onInitialize(params => { | ||
this.workspaceConfig = params.capabilities.workspace?.configuration ?? false; | ||
}); | ||
services.lsp.LanguageServer.onInitialized(_params => { | ||
const languages = this.sharedServices.ServiceRegistry.all; | ||
services.lsp.Connection?.client.register(DidChangeConfigurationNotification.type, { | ||
// Listen to configuration changes for all languages | ||
section: languages.map(lang => this.toSectionName(lang.LanguageMetaData.languageId)) | ||
}); | ||
}); | ||
} | ||
|
||
protected async initialize(): Promise<void> { | ||
const connection = this.sharedServices.lsp.Connection; | ||
if (this.workspaceConfig && connection) { | ||
const languages = this.sharedServices.ServiceRegistry.all; | ||
const configToUpdate: ConfigurationItem[] = languages.map(lang => { return { section: this.toSectionName(lang.LanguageMetaData.languageId) }; }); | ||
// get workspace configurations (default scope URI) | ||
const configs = await connection.workspace.getConfiguration(configToUpdate); | ||
configToUpdate.forEach((conf, idx) => { | ||
this.updateSectionConfiguration(conf.section!, configs[idx]); | ||
}); | ||
} | ||
this.initialized = true; | ||
} | ||
|
||
updateConfiguration(change: DidChangeConfigurationParams): void { | ||
if(!change.settings) { | ||
return; | ||
} | ||
Object.keys(change.settings).forEach(section => { | ||
this.updateSectionConfiguration(section, change.settings[section]); | ||
}); | ||
} | ||
|
||
protected updateSectionConfiguration(section: string, configuration: any): void { | ||
this.settings[section] = configuration; | ||
} | ||
|
||
async getConfiguration(language: string, configuration: string): Promise<any> { | ||
if (!this.initialized) { | ||
await this.initialize(); | ||
} | ||
const sectionName = this.toSectionName(language); | ||
if (!this.settings[sectionName]) { | ||
this.settings[sectionName] = {}; | ||
} | ||
return this.settings[sectionName][configuration]; | ||
} | ||
|
||
protected toSectionName(languageId: string): string { | ||
return `${languageId}`; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters