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

Content Collections Intellisense #915

Merged
merged 36 commits into from
Aug 15, 2024

Conversation

Princesseuh
Copy link
Member

@Princesseuh Princesseuh commented Jul 24, 2024

Changes

Requires withastro/astro#11639 to land first

Adds support for intellisense inside Markdown, MDX and Markdoc. Everything expected is supported, diagnostics, completions, hover, go to definition / references (both on the .md/.mdx/.mdoc side and the .ts side), etc.

Currently, this is behind a astro.content-intellisense flag as the feature in core is also experimental.

Testing

Added tests

Docs

Will be documented on the Astro part of things mostly

Copy link

changeset-bot bot commented Jul 24, 2024

🦋 Changeset detected

Latest commit: afda8cb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@astrojs/language-server Minor
@astrojs/ts-plugin Minor
@astrojs/yaml2ts Minor
astro-vscode Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -22,7 +22,7 @@
"@changesets/cli": "^2.26.1",
"prettier": "^3.2.5",
"turbo": "1.10.2",
"typescript": "^5.2.2",
"typescript": "^5.5.4",
Copy link
Member Author

Choose a reason for hiding this comment

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

Unrelated to the PR, but I was getting a wonky TS error somewhere and updating TS fixed it.

@Princesseuh Princesseuh marked this pull request as ready for review August 8, 2024 23:08
// The vast majority of clients support workspaceFolders, but notably our tests currently don't
// Ref: https://github.com/volarjs/volar.js/issues/229
const folders =
params.workspaceFolders ?? (params.rootUri ? [{ uri: params.rootUri }] : []) ?? [];
Copy link
Member Author

Choose a reason for hiding this comment

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

This brought me back to 2 years ago when you actually needed to handle rootURI. Good times (no)

}

if (hasChanges) {
// TODO: Figure out how to refresh the diagnostics
Copy link
Member Author

Choose a reason for hiding this comment

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

Couldn't figure out a way to do this from a Volar service, will have to ask Johnson. It's not too impactful, as soon as you trigger any actions that refresh the diagnostics (open the file, type anything etc) it refreshes normally.


return {
...yamlPluginInstance,
// Disable codelenses, we'll provide our own
Copy link
Member Author

Choose a reason for hiding this comment

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

Not included in this PR, but disabling the built-in code lenses is still valuable, they're irrelevant for Astro users.

Comment on lines -1 to -4
---
import { getEntryBySlug } from 'astro:content';
getEntryBySlug;
---
Copy link
Member Author

Choose a reason for hiding this comment

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

This test no longer work since we now generate types in the tests. That's okay because the feature being tested here is still tested, just not.. double tested

@@ -2,6 +2,9 @@
"name": "astro-language-server-test",
"private": true,
"devDependencies": {
"astro": "^4.1.0"
"astro": "0.0.0-content-collections-intellisense-20240808223933"
Copy link
Member Author

Choose a reason for hiding this comment

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

To replace with the actual version once it's out.

Comment on lines +26 to +28
if (collectionConfig) {
languagePlugins.push(getFrontmatterLanguagePlugin([collectionConfig]));
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Unfortunately in TS plugins getting any kind of config (apart from tsconfig.json) is really annoying. As such, the feature is always enabled there when it finds a config.

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

I think I more or less / maybe / a little bit grasp what's going on, but otherwise the code generally looks good to me.

packages/ts-plugin/src/frontmatter.ts Outdated Show resolved Hide resolved
Copy link
Member

@ematipico ematipico left a comment

Choose a reason for hiding this comment

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

Left questions. Few things:

  • there's a bunch of repeated code, can that be shared?
  • there are few magic numbers that don't have any comment associated that explain them
  • I've seen many bang operators (e.g. yamlPluginInstance.provide!['yaml/languageService']) used. It would be great to add a // SAFETY comment that explains why they are safe to use

packages/yaml2ts/src/yaml2ts.ts Outdated Show resolved Hide resolved
packages/yaml2ts/src/yaml2ts.ts Outdated Show resolved Hide resolved
const valueKey = JSON.stringify(item.key.toJS(frontmatterContent));

frontmatterMappings.push({
generatedOffsets: [fullResult.length + objectContent.length],
Copy link
Member

Choose a reason for hiding this comment

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

nit: I prefer to see fullResult.length being passed as parameter, so the function stays pure.

packages/yaml2ts/src/yaml2ts.ts Outdated Show resolved Hide resolved
}

if (isSeq(item)) {
mapSeq(item);
Copy link
Member

Choose a reason for hiding this comment

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

nit: I wish we could avoid recursion... 😅

packages/language-server/src/core/frontmatterHolders.ts Outdated Show resolved Hide resolved
packages/language-server/src/plugins/yaml.ts Outdated Show resolved Hide resolved
packages/language-server/src/plugins/yaml.ts Outdated Show resolved Hide resolved
packages/ts-plugin/src/frontmatter.ts Outdated Show resolved Hide resolved
Comment on lines +11 to +13
const SUPPORTED_FRONTMATTER_EXTENSIONS = { md: 'markdown', mdx: 'mdx', mdoc: 'mdoc' };
const SUPPORTED_FRONTMATTER_EXTENSIONS_KEYS = Object.keys(SUPPORTED_FRONTMATTER_EXTENSIONS);
const SUPPORTED_FRONTMATTER_EXTENSIONS_VALUES = Object.values(SUPPORTED_FRONTMATTER_EXTENSIONS);
Copy link
Member

Choose a reason for hiding this comment

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

This is repeated code, any chance to make this shared?

Copy link
Member Author

@Princesseuh Princesseuh Aug 9, 2024

Choose a reason for hiding this comment

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

It's a bit wonky to make shared because it's in different packages with no link between each others apart from one and it doesn't really belong in the YAML 2 TS package since it doesn't deal with the supported languages, but I guess it could be there

@Princesseuh Princesseuh merged commit d624646 into main Aug 15, 2024
4 checks passed
@Princesseuh Princesseuh deleted the feat/content-collections-intellisense branch August 15, 2024 10:15
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