-
-
Notifications
You must be signed in to change notification settings - Fork 52
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
fix: scripts affecting intellisense #900
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
42294bc
fix: scripts affecting intellisense
Princesseuh 58de210
test: add tests from #897
Princesseuh d894945
feat: update compiler
Princesseuh cb46ee1
chore: changeset
Princesseuh db9f309
Merge branch 'main' into fix/script-intellisense
Princesseuh 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
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,6 @@ | ||
--- | ||
"@astrojs/language-server": minor | ||
"astro-vscode": minor | ||
--- | ||
|
||
Improves the handling of script and style tags. This release fixes numerous issues where the presence of those tags could break intellisense in certain parts of the file. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,169 +1,45 @@ | ||
import type { ParentNode, ParseResult } from '@astrojs/compiler/types'; | ||
import { is } from '@astrojs/compiler/utils'; | ||
import type { TSXExtractedStyle } from '@astrojs/compiler/types'; | ||
import type { CodeInformation, VirtualCode } from '@volar/language-core'; | ||
import { Segment, toString } from 'muggle-string'; | ||
import type ts from 'typescript'; | ||
import type { HTMLDocument, Node } from 'vscode-html-languageservice'; | ||
import { buildMappings } from '../buildMappings.js'; | ||
import type { AttributeNodeWithPosition } from './compilerUtils.js'; | ||
|
||
export function extractStylesheets( | ||
snapshot: ts.IScriptSnapshot, | ||
htmlDocument: HTMLDocument, | ||
ast: ParseResult['ast'] | ||
): VirtualCode[] { | ||
const embeddedCSSCodes: VirtualCode[] = findEmbeddedStyles(snapshot, htmlDocument.roots); | ||
|
||
const inlineStyles = findInlineStyles(ast); | ||
if (inlineStyles.length > 0) { | ||
const codes: Segment<CodeInformation>[] = []; | ||
for (const inlineStyle of inlineStyles) { | ||
codes.push('x { '); | ||
codes.push([ | ||
inlineStyle.value, | ||
undefined, | ||
inlineStyle.position.start.offset + 'style="'.length, | ||
{ | ||
completion: true, | ||
verification: false, | ||
semantic: true, | ||
navigation: true, | ||
structure: true, | ||
format: false, | ||
}, | ||
]); | ||
codes.push(' }\n'); | ||
} | ||
|
||
const mappings = buildMappings(codes); | ||
const text = toString(codes); | ||
|
||
embeddedCSSCodes.push({ | ||
id: 'inline.css', | ||
languageId: 'css', | ||
snapshot: { | ||
getText: (start, end) => text.substring(start, end), | ||
getLength: () => text.length, | ||
getChangeRange: () => undefined, | ||
}, | ||
embeddedCodes: [], | ||
mappings, | ||
}); | ||
} | ||
|
||
return embeddedCSSCodes; | ||
} | ||
|
||
/** | ||
* Find all embedded styles in a document. | ||
* Embedded styles are styles that are defined in `<style>` tags. | ||
*/ | ||
function findEmbeddedStyles(snapshot: ts.IScriptSnapshot, roots: Node[]): VirtualCode[] { | ||
const embeddedCSSCodes: VirtualCode[] = []; | ||
let cssIndex = 0; | ||
|
||
getEmbeddedCSSInNodes(roots); | ||
|
||
function getEmbeddedCSSInNodes(nodes: Node[]) { | ||
for (const [_, node] of nodes.entries()) { | ||
if ( | ||
node.tag === 'style' && | ||
node.startTagEnd !== undefined && | ||
node.endTagStart !== undefined | ||
) { | ||
const styleText = snapshot.getText(node.startTagEnd, node.endTagStart); | ||
embeddedCSSCodes.push({ | ||
id: `${cssIndex}.css`, | ||
languageId: 'css', | ||
snapshot: { | ||
getText: (start, end) => styleText.substring(start, end), | ||
getLength: () => styleText.length, | ||
getChangeRange: () => undefined, | ||
}, | ||
mappings: [ | ||
{ | ||
sourceOffsets: [node.startTagEnd], | ||
generatedOffsets: [0], | ||
lengths: [styleText.length], | ||
data: { | ||
verification: false, | ||
completion: true, | ||
semantic: true, | ||
navigation: true, | ||
structure: true, | ||
format: false, | ||
}, | ||
}, | ||
], | ||
embeddedCodes: [], | ||
}); | ||
cssIndex++; | ||
} | ||
|
||
if (node.children) getEmbeddedCSSInNodes(node.children); | ||
} | ||
} | ||
|
||
return embeddedCSSCodes; | ||
export function extractStylesheets(styles: TSXExtractedStyle[]): VirtualCode { | ||
return mergeCSSContexts(styles); | ||
} | ||
|
||
/** | ||
* Find all inline styles using the Astro AST | ||
* Inline styles are styles that are defined in the `style` attribute of an element. | ||
* TODO: Merge this with `findEmbeddedCSS`? Unlike scripts, there's no scoping being done here, so merging all of it in | ||
* the same virtual file is possible, though it might make mapping more tricky. | ||
*/ | ||
function findInlineStyles(ast: ParseResult['ast']): AttributeNodeWithPosition[] { | ||
const styleAttrs: AttributeNodeWithPosition[] = []; | ||
|
||
// `@astrojs/compiler`'s `walk` method is async, so we can't use it here. Arf | ||
function walkDown(parent: ParentNode) { | ||
if (!parent.children) return; | ||
|
||
parent.children.forEach((child) => { | ||
if (is.element(child)) { | ||
const styleAttribute = child.attributes.find( | ||
(attr) => attr.name === 'style' && attr.kind === 'quoted' | ||
); | ||
|
||
if (styleAttribute && styleAttribute.position) { | ||
styleAttrs.push(styleAttribute as AttributeNodeWithPosition); | ||
} | ||
} | ||
|
||
if (is.parent(child)) { | ||
walkDown(child); | ||
} | ||
}); | ||
} | ||
|
||
walkDown(ast); | ||
|
||
return styleAttrs; | ||
} | ||
|
||
// TODO: Provide completion for classes and IDs | ||
export function collectClassesAndIdsFromDocument(ast: ParseResult['ast']): string[] { | ||
const classesAndIds: string[] = []; | ||
function walkDown(parent: ParentNode) { | ||
if (!parent.children) return; | ||
|
||
parent.children.forEach((child) => { | ||
if (is.element(child)) { | ||
const classOrIDAttributes = child.attributes | ||
.filter((attr) => attr.kind === 'quoted' && (attr.name === 'class' || attr.name === 'id')) | ||
.flatMap((attr) => attr.value.split(' ')); | ||
|
||
classesAndIds.push(...classOrIDAttributes); | ||
} | ||
|
||
if (is.parent(child)) { | ||
walkDown(child); | ||
} | ||
}); | ||
function mergeCSSContexts(inlineStyles: TSXExtractedStyle[]): VirtualCode { | ||
const codes: Segment<CodeInformation>[] = []; | ||
|
||
for (const javascriptContext of inlineStyles) { | ||
if (javascriptContext.type === 'style-attribute') codes.push('__ { '); | ||
codes.push([ | ||
javascriptContext.content, | ||
undefined, | ||
javascriptContext.position.start, | ||
{ | ||
verification: true, | ||
completion: true, | ||
semantic: true, | ||
navigation: true, | ||
structure: true, | ||
format: false, | ||
}, | ||
]); | ||
if (javascriptContext.type === 'style-attribute') codes.push(' }\n'); | ||
} | ||
|
||
walkDown(ast); | ||
|
||
return classesAndIds; | ||
const mappings = buildMappings(codes); | ||
const text = toString(codes); | ||
|
||
return { | ||
id: 'style.css', | ||
languageId: 'css', | ||
snapshot: { | ||
getText: (start, end) => text.substring(start, end), | ||
getLength: () => text.length, | ||
getChangeRange: () => undefined, | ||
}, | ||
embeddedCodes: [], | ||
mappings, | ||
}; | ||
} |
Oops, something went wrong.
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.
This is the case now in the new format, which improves intellisense for class names and stuff when you have multiple style tags in the same file. Kinda unlikely, but still funny!