-
Notifications
You must be signed in to change notification settings - Fork 42k
Contribute to html language server with a custom language. #146731
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
Merged
Martin Aeschlimann (aeschli)
merged 8 commits into
microsoft:main
from
angelozerr:html-contributions
Jul 21, 2022
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d647fb1
Contribute to html language server with a custom language.
angelozerr 1ea505a
Merge branch 'main' into html-contributions
aeschli 3e65d4b
Merge branch 'main' into pr/angelozerr/146731
aeschli 00e2fdc
refactor out LanguageParticipants
aeschli 096438a
restart client on language selector change
aeschli ced2eda
htmlLanguage -> htmlLanguageParticipants
aeschli fb67b34
tune autoInsert wording
aeschli 290f919
tune autoInsert description
aeschli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Contribute to html language server with a custom language.
Fixes #146730 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
commit d647fb1e93001fe487bc9132c4a489d3be2ba8fb
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
149 changes: 149 additions & 0 deletions
149
extensions/html-language-features/client/src/htmlLanguageContribution.ts
This file contains hidden or 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,149 @@ | ||
| /*--------------------------------------------------------------------------------------------- | ||
| * Copyright (c) Microsoft Corporation. All rights reserved. | ||
| * Licensed under the MIT License. See License.txt in the project root for license information. | ||
| *--------------------------------------------------------------------------------------------*/ | ||
|
|
||
| import { DocumentSelector } from 'vscode-languageclient'; | ||
| import { isDeepStrictEqual } from 'util'; | ||
| import { commands, Disposable, env, extensions, window } from 'vscode'; | ||
|
|
||
| const RELOAD_WINDOW = 'workbench.action.reloadWindow'; | ||
|
|
||
| let existingExtensions: HtmlLanguageContribution[]; | ||
|
|
||
| /** | ||
| * Html language contribution. | ||
| */ | ||
| export interface HtmlLanguageContribution { | ||
| /** | ||
| * The language Id which contributes to the Html language server. | ||
| */ | ||
| languageId: string; | ||
| /** | ||
| * true if the language activates the auto insertion and false otherwise. | ||
| */ | ||
| autoInsert: false; | ||
| } | ||
|
|
||
| /** | ||
| * Returns all Html language contributions declared with 'contributes/htmlLanguages' from package.json: | ||
| * | ||
| *"contributes": { | ||
| * "languages": [ | ||
| * { | ||
| * "id": "handlebars", | ||
| * ... | ||
| * } | ||
| * ], | ||
| * "htmlLanguages": [ | ||
| * { | ||
| * "languageId": "handlebars", | ||
| * "autoInsert": true | ||
| * } | ||
| * ] | ||
| *} | ||
| * | ||
| * @param toDispose | ||
| * | ||
| * @returns all Html language contributions declared with 'contributes/htmlLanguages' from package.json. | ||
| */ | ||
| export function getHtmlLanguageContributions(toDispose?: Disposable[]): HtmlLanguageContribution[] { | ||
| const result: HtmlLanguageContribution[] = []; | ||
| for (const extension of extensions.all) { | ||
| const htmlLanguages = extension.packageJSON?.contributes?.htmlLanguages; | ||
| if (Array.isArray(htmlLanguages)) { | ||
| htmlLanguages.forEach(htmlLanguage => { | ||
| const htmlLanguageContribution = createHtmlLanguageContribution(htmlLanguage); | ||
| if (htmlLanguageContribution) { | ||
| result.push(htmlLanguageContribution); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| // Make a copy of extensions: | ||
| existingExtensions = result.slice(); | ||
|
|
||
| if (toDispose) { | ||
| toDispose.push(extensions.onDidChange(_ => { | ||
| handleExtensionChange(); | ||
| })); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| function handleExtensionChange(): void { | ||
| if (!existingExtensions) { | ||
| return; | ||
| } | ||
| const oldExtensions = new Set(existingExtensions.slice()); | ||
| const newExtensions = getHtmlLanguageContributions(); | ||
| let hasChanged = (oldExtensions.size !== newExtensions.length); | ||
| if (!hasChanged) { | ||
| for (const newExtension of newExtensions) { | ||
| let found = false; | ||
| for (const oldExtension of oldExtensions) { | ||
| if (isDeepStrictEqual(oldExtension, newExtension)) { | ||
| found = true; | ||
| break; | ||
| } | ||
| } | ||
| if (found) { | ||
| continue; | ||
| } else { | ||
| hasChanged = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (hasChanged) { | ||
| const msg = `Extensions to the Html Language Server changed, reloading ${env.appName} is required for the changes to take effect.`; | ||
| const action = 'Reload'; | ||
| window.showWarningMessage(msg, action).then((selection) => { | ||
| if (action === selection) { | ||
| commands.executeCommand(RELOAD_WINDOW); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns the document selector of the Html language client. | ||
| * | ||
| * The returned document selector contains the html languageId and and all languageId contained in `contributes/htmlLanguages` package.json. | ||
| * | ||
| * @param htmlContributions all Html language contributions from other VS Code extensions. | ||
| * | ||
| * @returns the document selector of the Html language client. | ||
| */ | ||
| export function getDocumentSelector(htmlContributions: HtmlLanguageContribution[]): DocumentSelector { | ||
| let documentSelector: DocumentSelector = ['html']; | ||
| htmlContributions.forEach((contribution: HtmlLanguageContribution) => { | ||
| documentSelector = documentSelector.concat(contribution.languageId); | ||
| }); | ||
| return documentSelector; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the auto insert support for each Html languages. | ||
| * | ||
| * @param htmlContributions all Html language contributions from other VS Code extensions. | ||
| * | ||
| * @returns the auto insert support for each Html languages. | ||
| */ | ||
| export function getSupportedLanguagesAutoInsert(htmlContributions: HtmlLanguageContribution[]): { [id: string]: boolean } { | ||
| const supportedLanguages: { [id: string]: boolean } = { html: true }; | ||
| htmlContributions.forEach((contribution: HtmlLanguageContribution) => { | ||
| supportedLanguages[contribution.languageId] = contribution.autoInsert; | ||
| }); | ||
| return supportedLanguages; | ||
| } | ||
|
|
||
| function createHtmlLanguageContribution(section: any): HtmlLanguageContribution | undefined { | ||
| const languageId: string | undefined = section.languageId; | ||
| if (!languageId) { | ||
| return; | ||
| } | ||
| const autoInsert = section.autoInsert === true ? true : false; | ||
| return { languageId, autoInsert } as HtmlLanguageContribution; | ||
| } |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API prefers that
undefinedequalsfalse, so maybenoAutoInsertis better because it conveys the default value?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want this to become a list of all features: 'hover', 'completions', .. and it looks much nicer if they are not negated. By default