Skip to content

Commit

Permalink
feat: account for new json schemas location
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-lefebvre committed Sep 11, 2024
1 parent b79ff82 commit 69a8067
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
8 changes: 8 additions & 0 deletions .changeset/hungry-nails-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@astrojs/language-server": minor
"@astrojs/ts-plugin": minor
---

Updates where to look for JSON Schemas for content intellisense.

In 5.0, schemas are moved from `.astro/` to `.astro/astro`. Checks are now made against both directories.
8 changes: 6 additions & 2 deletions packages/language-server/src/nodeServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ connection.onInitialize((params) => {
try {
const folderUri = URI.parse(folder.uri);
let config = server.fileSystem.readFile(
Utils.joinPath(folderUri, '.astro/collections/collections.json'),
Utils.joinPath(folderUri, '.astro/astro/collections/collections.json'),
);

if (!config) {
config = server.fileSystem.readFile(
Utils.joinPath(folderUri, '.astro/collections/collections.json'),
);
}
if (!config) {
return [];
}
Expand Down
33 changes: 23 additions & 10 deletions packages/ts-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,40 @@ import type { CollectionConfig } from './frontmatter.js';
import { getFrontmatterLanguagePlugin } from './frontmatter.js';
import { getLanguagePlugin } from './language.js';

export = createLanguageServicePlugin((ts, info) => {
let collectionConfig = undefined;

function getCollectionConfig(
readFile: (path: string) => string | undefined,
): CollectionConfig['config'] | undefined {
try {
const currentDir = info.project.getCurrentDirectory();
const fileContent = ts.sys.readFile(currentDir + '/.astro/collections/collections.json');
let fileContent = readFile('/.astro/astro/collections/collections.json');
if (fileContent) {
return JSON.parse(fileContent);
}
fileContent = readFile('/.astro/collections/collections.json');
if (fileContent) {
collectionConfig = {
folder: currentDir,
config: JSON.parse(fileContent) as CollectionConfig['config'],
};
return JSON.parse(fileContent);
}
} catch (err) {
// If the file doesn't exist, we don't really care, but if it's something else, we want to know
if (err && (err as any).code !== 'ENOENT') console.error(err);
}
}

export = createLanguageServicePlugin((ts, info) => {
const currentDir = info.project.getCurrentDirectory();

const collectionConfig = getCollectionConfig((path) => ts.sys.readFile(currentDir + path));

let languagePlugins: LanguagePlugin<string>[] = [getLanguagePlugin()];

if (collectionConfig) {
languagePlugins.push(getFrontmatterLanguagePlugin([collectionConfig]));
languagePlugins.push(
getFrontmatterLanguagePlugin([
{
folder: currentDir,
config: collectionConfig,
},
]),
);
}

return {
Expand Down

0 comments on commit 69a8067

Please sign in to comment.