-
-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathDocument.ts
More file actions
183 lines (164 loc) · 5.56 KB
/
Document.ts
File metadata and controls
183 lines (164 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { urlToPath } from '../../utils';
import { WritableDocument } from './DocumentBase';
import { extractScriptTags, extractStyleTag, extractTemplateTag, TagInformation } from './utils';
import { parseHtml } from './parseHtml';
import { SvelteConfig, configLoader } from './configLoader';
import { HTMLDocument } from 'vscode-html-languageservice';
import { Range } from 'vscode-languageserver';
import { importSvelte } from '../../importPackage';
/**
* Represents a text document contains a svelte component.
*/
export class Document extends WritableDocument {
languageId = 'svelte';
scriptInfo: TagInformation | null = null;
moduleScriptInfo: TagInformation | null = null;
styleInfo: TagInformation | null = null;
templateInfo: TagInformation | null = null;
configPromise: Promise<SvelteConfig | undefined>;
config?: SvelteConfig;
html!: HTMLDocument;
openedByClient = false;
/**
* Compute and cache directly because of performance reasons
* and it will be called anyway.
*/
private path = urlToPath(this.url);
private _compiler: typeof import('svelte/compiler') | undefined;
get compiler() {
return this.getCompiler();
}
private svelteVersion: [number, number] | undefined;
public get isSvelte5() {
return this.getSvelteVersion()[0] > 4;
}
constructor(
public url: string,
public content: string,
loadConfig = false
) {
super();
this.configPromise = loadConfig
? configLoader.awaitConfig(this.getFilePath() || '')
: Promise.resolve(undefined);
this.updateDocInfo();
}
private getCompiler() {
if (!this._compiler) {
this._compiler = importSvelte(this.getFilePath() || '');
}
return this._compiler;
}
private updateDocInfo() {
this.html = parseHtml(this.content);
const update = (config: SvelteConfig | undefined) => {
const scriptTags = extractScriptTags(this.content, this.html);
this.config = config;
this.scriptInfo = this.addDefaultLanguage(config, scriptTags?.script || null, 'script');
this.moduleScriptInfo = this.addDefaultLanguage(
config,
scriptTags?.moduleScript || null,
'script'
);
this.styleInfo = this.addDefaultLanguage(
config,
extractStyleTag(this.content, this.html),
'style'
);
this.templateInfo = this.addDefaultLanguage(
config,
extractTemplateTag(this.content, this.html),
'markup'
);
};
const config = configLoader.getConfig(this.getFilePath() || '');
if (config && !config.loadConfigError) {
update(config);
} else {
update(undefined);
this.configPromise.then((c) => update(c));
}
}
getSvelteVersion() {
if (!this.svelteVersion) {
const [major, minor] = this.compiler.VERSION.split('.');
this.svelteVersion = [Number(major), Number(minor)];
}
return this.svelteVersion;
}
/**
* Get text content
*/
getText(range?: Range): string {
if (range) {
return this.content.substring(this.offsetAt(range.start), this.offsetAt(range.end));
}
return this.content;
}
/**
* Set text content and increase the document version
*/
setText(text: string) {
this.content = text;
this.version++;
this.lineOffsets = undefined;
this.updateDocInfo();
}
/**
* Returns the file path if the url scheme is file
*/
getFilePath(): string | null {
return this.path;
}
/**
* Get URL file path.
*/
getURL() {
return this.url;
}
/**
* Returns the language associated to script, style or template.
* Returns an empty string if there's nothing set.
*/
getLanguageAttribute(tag: 'script' | 'style' | 'template'): string {
const attrs =
(tag === 'style'
? this.styleInfo?.attributes
: tag === 'script'
? this.scriptInfo?.attributes || this.moduleScriptInfo?.attributes
: this.templateInfo?.attributes) || {};
const lang = attrs.lang || attrs.type || '';
return lang.replace(/^text\//, '');
}
/**
* Returns true if there's `lang="X"` on script or style or template.
*/
hasLanguageAttribute(): boolean {
return (
!!this.getLanguageAttribute('script') ||
!!this.getLanguageAttribute('style') ||
!!this.getLanguageAttribute('template')
);
}
/**
* @deprecated This no longer exists in svelte-preprocess v5, we leave it in in case someone is using this with v4
*/
private addDefaultLanguage(
config: SvelteConfig | undefined,
tagInfo: TagInformation | null,
tag: 'style' | 'script' | 'markup'
): TagInformation | null {
if (!tagInfo || !config) {
return tagInfo;
}
const defaultLang = Array.isArray(config.preprocess)
? config.preprocess.find((group) => group.defaultLanguages?.[tag])?.defaultLanguages?.[
tag
]
: config.preprocess?.defaultLanguages?.[tag];
if (!tagInfo.attributes.lang && !tagInfo.attributes.type && defaultLang) {
tagInfo.attributes.lang = defaultLang;
}
return tagInfo;
}
}