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

Asynchronous TS plugin example #2

Closed
wants to merge 24 commits into from
Closed

Conversation

johnsoncodehk
Copy link
Owner

See vuejs/language-tools#3789

Different from vue langauge tools, mdx-analyzer needs to load the plugin asynchronously, so decorateLanguageService and decorateLanguageServiceHost are called lazily.

packages/vscode-mdx/src/typescript-plugin.js Outdated Show resolved Hide resolved
packages/vscode-mdx/src/typescript-plugin.js Outdated Show resolved Hide resolved
}
info.languageServiceHost.getScriptVersion = (fileName) => {
if (!initialized && fileName.endsWith('.mdx')) {
return 'initializing...'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be any string, as long as it’s not numeric, right?

Suggested change
return 'initializing...'
return 'initializing'

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

if (getProjectVersion) {
info.languageServiceHost.getProjectVersion = () => {
if (!initialized) {
return getProjectVersion() + ',initializing...'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is rendered in the VS Code status bar, right?

Let’s use a real ellipsis and proper spacing.

Suggested change
return getProjectVersion() + ',initializing...'
return getProjectVersion() + ', initializing'

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will not be shown to the user. The purpose of patching getProjectVersion is just so that after the initialization is completed, the language service knows that the cache needs to be refreshed.

packages/vscode-mdx/src/typescript-plugin.js Outdated Show resolved Hide resolved
],
"typescriptServerPlugins": [
{
"name": "typescript-plugin-bundled",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to come from node_modules? Or can we also point to a relative path?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packages/vscode-mdx/src/typescript-plugin.js Outdated Show resolved Hide resolved
} from '@volar/typescript'
import {createFileProvider, resolveCommonLanguageId} from '@volar/language-core'
import {getLanguageModule} from '@mdx-js/language-service'
import {loadPlugins} from '../../language-server/lib/configuration.js'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be nicer to move this function into @mdx-js/language-service. Although it’s not ideal loadPlugins depends on Node.js APIs.

@@ -0,0 +1,107 @@
/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice to publish this as @mdx-js/typescript-plugin, but we can do that later.

packages/vscode-mdx/src/typescript-plugin.js Outdated Show resolved Hide resolved
johnsoncodehk and others added 10 commits December 11, 2023 00:34
Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
Signed-off-by: Johnson Chu <johnsoncodehk@gmail.com>
Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
Signed-off-by: Johnson Chu <johnsoncodehk@gmail.com>
Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
Signed-off-by: Johnson Chu <johnsoncodehk@gmail.com>
Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
Signed-off-by: Johnson Chu <johnsoncodehk@gmail.com>
Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
Signed-off-by: Johnson Chu <johnsoncodehk@gmail.com>
Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
Signed-off-by: Johnson Chu <johnsoncodehk@gmail.com>
Volar 2 offers various small enhancements.

Co-authored-by: Remco Haszing <remcohaszing@gmail.com>
Before Volar, the ESM, JSX, and markdown parts of virtual files had to
map character by character, with parts shadowed by whitespace
characters. This was still how virtual files were built. However,
because of how Volar mappings work, this is unnecessary.

This change overhauls how virtual files are generated. This leads to
more flexibility of virtual files, meaning we can omit or inject
content.

This is already leveraged in several ways:

- Markdown nodes in MDX are represented by TypeScript fragments. This
  provides compatibility with JSX elements that require children on a
  type level.
- Markdown text nodes in MDX are represented by TypeScript JSX
  expressions containing an empty string.
- JavaScript chunks are represented in the virtual markdown files as
  HTML comments.
Formatting embedded content causes too much unexpected behaviour in MDX
files.

Closes mdx-js#355
If the Props type isn’t defined, it now falls back to an empty shape
instead of any. Also all keys are marked as readonly. As a result, props
are no longer represented using the string `Props`, but an inline
representation of the shape of the props.

Given that the upcoming support for an MDX layout also requires a
patched version of `Props`, this new setup is more consistent.

Also the `props` description now contains a link to the props page on
the MDX website.
Andarist and others added 4 commits December 13, 2023 19:02
Signed-off-by: Mateusz Burzyński <mateuszburzynski@gmail.com>
`addOffset` now handles generating content based on the original content
and offsets.
This can be used for example for bracket matching.
@remcohaszing
Copy link
Collaborator

Can you retarget this PR to https://github.com/mdx-js/mdx-analyzer? I think that would be better for visibility and discussions. I’m not even sure of the target repo of a PR can be changed.

remcohaszing and others added 9 commits December 14, 2023 13:17
MDX allows to use indentation. In regular markdown however, indented
code represents a code block. To work around this, all virtual markdown
chunks are now dedented. This may change the semantic meaning of certain
markdown, but it doesn’t affect editor features as far as I’m aware.
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
@johnsoncodehk
Copy link
Owner Author

Here it's: mdx-js#371

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants