summaryrefslogtreecommitdiff
path: root/tools/language-server/src/plugins/css/CSSDocument.ts
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2021-06-16 13:20:29 -0500
committerGravatar GitHub <noreply@github.com> 2021-06-16 13:20:29 -0500
commit0dd278810e4353799c7239463f156b358ea30871 (patch)
tree964a62a13467950509b446ebeaa9b668e5874665 /tools/language-server/src/plugins/css/CSSDocument.ts
parent382868abfc4b46a0c582ec4bf868719d4e87bbd2 (diff)
downloadastro-0dd278810e4353799c7239463f156b358ea30871.tar.gz
astro-0dd278810e4353799c7239463f156b358ea30871.tar.zst
astro-0dd278810e4353799c7239463f156b358ea30871.zip
Fix VS Code extension (#467)
* chore: astro-languageserver => @astrojs/language-server * chore: astro-vscode => vscode * chore: move devDeps to deps * chore: bump language-server to 0.5.0-next.0 * chore: remove astro-docs * chore: update changelog * fix: expose `astro-ls` bin * fix: vscode extension * chore: update changelog
Diffstat (limited to 'tools/language-server/src/plugins/css/CSSDocument.ts')
-rw-r--r--tools/language-server/src/plugins/css/CSSDocument.ts95
1 files changed, 95 insertions, 0 deletions
diff --git a/tools/language-server/src/plugins/css/CSSDocument.ts b/tools/language-server/src/plugins/css/CSSDocument.ts
new file mode 100644
index 000000000..9f1839678
--- /dev/null
+++ b/tools/language-server/src/plugins/css/CSSDocument.ts
@@ -0,0 +1,95 @@
+import { Stylesheet, TextDocument } from 'vscode-css-languageservice';
+import { Position } from 'vscode-languageserver';
+import { getLanguageService } from './service';
+import { Document, DocumentMapper, ReadableDocument, TagInformation } from '../../core/documents/index';
+
+export interface CSSDocumentBase extends DocumentMapper, TextDocument {
+ languageId: string;
+ stylesheet: Stylesheet;
+}
+
+export class CSSDocument extends ReadableDocument implements DocumentMapper {
+ private styleInfo: Pick<TagInformation, 'attributes' | 'start' | 'end'>;
+ readonly version = this.parent.version;
+
+ public stylesheet: Stylesheet;
+ public languageId: string;
+
+ constructor(private parent: Document) {
+ super();
+
+ if (this.parent.styleInfo) {
+ this.styleInfo = this.parent.styleInfo;
+ } else {
+ this.styleInfo = {
+ attributes: {},
+ start: -1,
+ end: -1,
+ };
+ }
+
+ this.languageId = this.language;
+ this.stylesheet = getLanguageService(this.language).parseStylesheet(this);
+ }
+
+ /**
+ * Get the fragment position relative to the parent
+ * @param pos Position in fragment
+ */
+ getOriginalPosition(pos: Position): Position {
+ const parentOffset = this.styleInfo.start + this.offsetAt(pos);
+ return this.parent.positionAt(parentOffset);
+ }
+
+ /**
+ * Get the position relative to the start of the fragment
+ * @param pos Position in parent
+ */
+ getGeneratedPosition(pos: Position): Position {
+ const fragmentOffset = this.parent.offsetAt(pos) - this.styleInfo.start;
+ return this.positionAt(fragmentOffset);
+ }
+
+ /**
+ * Returns true if the given parent position is inside of this fragment
+ * @param pos Position in parent
+ */
+ isInGenerated(pos: Position): boolean {
+ const offset = this.parent.offsetAt(pos);
+ return offset >= this.styleInfo.start && offset <= this.styleInfo.end;
+ }
+
+ /**
+ * Get the fragment text from the parent
+ */
+ getText(): string {
+ return this.parent.getText().slice(this.styleInfo.start, this.styleInfo.end);
+ }
+
+ /**
+ * Returns the length of the fragment as calculated from the start and end positon
+ */
+ getTextLength(): number {
+ return this.styleInfo.end - this.styleInfo.start;
+ }
+
+ /**
+ * Return the parent file path
+ */
+ getFilePath(): string | null {
+ return this.parent.getFilePath();
+ }
+
+ getURL() {
+ return this.parent.getURL();
+ }
+
+ getAttributes() {
+ return this.styleInfo.attributes;
+ }
+
+ private get language() {
+ const attrs = this.getAttributes();
+ return attrs.lang || attrs.type || 'css';
+ }
+}