-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add astro support for VSCode extension (#3475)
* feat: add astro syntax grammar support * feat: add astro support for language server
- Loading branch information
Showing
15 changed files
with
195 additions
and
41 deletions.
There are no files selected for viewing
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,7 @@ | ||
--- | ||
'graphql-language-service-server': minor | ||
'vscode-graphql': minor | ||
'vscode-graphql-syntax': minor | ||
--- | ||
|
||
Add Astro file support |
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 |
---|---|---|
|
@@ -65,6 +65,8 @@ yoshiakis | |
|
||
// packages and tools | ||
argparse | ||
astro | ||
astrojs | ||
changesets | ||
clsx | ||
codemirror | ||
|
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
69 changes: 69 additions & 0 deletions
69
packages/graphql-language-service-server/src/parsers/astro.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,69 @@ | ||
import { parse } from 'astrojs-compiler-sync'; | ||
import { Position, Range } from 'graphql-language-service'; | ||
import { RangeMapper, SourceParser } from './types'; | ||
import { babelParser } from './babel'; | ||
|
||
type ParseAstroResult = | ||
| { type: 'error'; errors: string[] } | ||
| { | ||
type: 'ok'; | ||
scriptOffset: number; | ||
scriptAst: any[]; | ||
}; | ||
|
||
function parseAstro(source: string): ParseAstroResult { | ||
// eslint-disable-next-line unicorn/no-useless-undefined | ||
const { ast, diagnostics } = parse(source, undefined); | ||
if (diagnostics.some(d => d.severity === /* Error */ 1)) { | ||
return { | ||
type: 'error', | ||
errors: diagnostics.map(d => JSON.stringify(d)), | ||
}; | ||
} | ||
|
||
for (const node of ast.children) { | ||
if (node.type === 'frontmatter') { | ||
try { | ||
return { | ||
type: 'ok', | ||
scriptOffset: (node.position?.start.line ?? 1) - 1, | ||
scriptAst: [babelParser(node.value, ['typescript'])], | ||
}; | ||
} catch (error) { | ||
return { | ||
type: 'error', | ||
errors: [String(error)], | ||
}; | ||
} | ||
} | ||
} | ||
|
||
return { type: 'error', errors: ['Could not find frontmatter block'] }; | ||
} | ||
|
||
export const astroParser: SourceParser = (text, uri, logger) => { | ||
const parseAstroResult = parseAstro(text); | ||
if (parseAstroResult.type === 'error') { | ||
logger.error( | ||
`Could not parse the astro file at ${uri} to extract the graphql tags:`, | ||
); | ||
for (const error of parseAstroResult.errors) { | ||
logger.error(String(error)); | ||
} | ||
return null; | ||
} | ||
|
||
const rangeMapper: RangeMapper = range => { | ||
return new Range( | ||
new Position( | ||
range.start.line + parseAstroResult.scriptOffset, | ||
range.start.character, | ||
), | ||
new Position( | ||
range.end.line + parseAstroResult.scriptOffset, | ||
range.end.character, | ||
), | ||
); | ||
}; | ||
return { asts: parseAstroResult.scriptAst, rangeMapper }; | ||
}; |
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
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
Oops, something went wrong.