-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathindex.ts
173 lines (163 loc) · 6.45 KB
/
index.ts
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
import { TextDocument, Position, Range } from 'vscode-languageserver-types';
import {
getCSSLanguageService,
getSCSSLanguageService,
getLESSLanguageService,
LanguageService
} from 'vscode-css-languageservice';
import * as _ from 'lodash';
import * as emmet from 'vscode-emmet-helper';
import { Priority } from './emmet';
import { LanguageModelCache, getLanguageModelCache } from '../../embeddedSupport/languageModelCache';
import { LanguageMode } from '../../embeddedSupport/languageModes';
import { VueDocumentRegions, LanguageId } from '../../embeddedSupport/embeddedSupport';
import { getFileFsPath } from '../../utils/paths';
import { prettierify } from '../../utils/prettier';
import { ParserOption } from '../../utils/prettier/prettier.d';
import { NULL_HOVER } from '../nullMode';
import { VLSFormatConfig } from '../../config';
export function getCSSMode(documentRegions: LanguageModelCache<VueDocumentRegions>): LanguageMode {
const languageService = getCSSLanguageService();
return getStyleMode('css', languageService, documentRegions);
}
export function getPostCSSMode(documentRegions: LanguageModelCache<VueDocumentRegions>): LanguageMode {
const languageService = getCSSLanguageService();
return getStyleMode('postcss', languageService, documentRegions);
}
export function getSCSSMode(documentRegions: LanguageModelCache<VueDocumentRegions>): LanguageMode {
const languageService = getSCSSLanguageService();
return getStyleMode('scss', languageService, documentRegions);
}
export function getLESSMode(documentRegions: LanguageModelCache<VueDocumentRegions>): LanguageMode {
const languageService = getLESSLanguageService();
return getStyleMode('less', languageService, documentRegions);
}
function getStyleMode(
languageId: LanguageId,
languageService: LanguageService,
documentRegions: LanguageModelCache<VueDocumentRegions>
): LanguageMode {
const embeddedDocuments = getLanguageModelCache(10, 60, document =>
documentRegions.refreshAndGet(document).getSingleLanguageDocument(languageId)
);
const stylesheets = getLanguageModelCache(10, 60, document => languageService.parseStylesheet(document));
let config: any = {};
return {
getId() {
return languageId;
},
configure(c) {
languageService.configure(c && c.css);
config = c;
},
doValidation(document) {
if (languageId === 'postcss') {
return [];
} else {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.doValidation(embedded, stylesheets.refreshAndGet(embedded));
}
},
doComplete(document, position) {
const embedded = embeddedDocuments.refreshAndGet(document);
const emmetSyntax = languageId === 'postcss' ? 'css' : languageId;
const lsCompletions = languageService.doComplete(embedded, position, stylesheets.refreshAndGet(embedded));
const lsItems = lsCompletions
? _.map(lsCompletions.items, i => {
return {
...i,
sortText: Priority.Platform + i.label
};
})
: [];
const emmetCompletions = emmet.doComplete(document, position, emmetSyntax, config.emmet);
if (!emmetCompletions) {
return { isIncomplete: false, items: lsItems };
} else {
const emmetItems = emmetCompletions.items.map(i => {
return {
...i,
sortText: Priority.Emmet + i.label
};
});
return {
isIncomplete: emmetCompletions.isIncomplete,
items: _.concat(emmetItems, lsItems)
};
}
},
doHover(document, position) {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.doHover(embedded, position, stylesheets.refreshAndGet(embedded)) || NULL_HOVER;
},
findDocumentHighlight(document, position) {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.findDocumentHighlights(embedded, position, stylesheets.refreshAndGet(embedded));
},
findDocumentSymbols(document) {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.findDocumentSymbols(embedded, stylesheets.refreshAndGet(embedded));
},
findDefinition(document, position) {
const embedded = embeddedDocuments.refreshAndGet(document);
const definition = languageService.findDefinition(embedded, position, stylesheets.refreshAndGet(embedded));
if (!definition) {
return [];
}
return definition;
},
findReferences(document, position) {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.findReferences(embedded, position, stylesheets.refreshAndGet(embedded));
},
findDocumentColors(document) {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.findDocumentColors(embedded, stylesheets.refreshAndGet(embedded));
},
getColorPresentations(document, color, range) {
const embedded = embeddedDocuments.refreshAndGet(document);
return languageService.getColorPresentations(embedded, stylesheets.refreshAndGet(embedded), color, range);
},
format(document, currRange, formattingOptions) {
if (config.vetur.format.defaultFormatter[languageId] === 'none') {
return [];
}
const { value, range } = getValueAndRange(document, currRange);
const needIndent = config.vetur.format.styleInitialIndent;
const parserMap: { [k: string]: ParserOption } = {
css: 'css',
postcss: 'css',
scss: 'scss',
less: 'less'
};
return prettierify(
value,
getFileFsPath(document.uri),
range,
config.vetur.format as VLSFormatConfig,
parserMap[languageId],
needIndent
);
},
onDocumentRemoved(document) {
embeddedDocuments.onDocumentRemoved(document);
stylesheets.onDocumentRemoved(document);
},
dispose() {
embeddedDocuments.dispose();
stylesheets.dispose();
}
};
}
function getValueAndRange(document: TextDocument, currRange: Range): { value: string; range: Range } {
let value = document.getText();
let range = currRange;
if (currRange) {
const startOffset = document.offsetAt(currRange.start);
const endOffset = document.offsetAt(currRange.end);
value = value.substring(startOffset, endOffset);
} else {
range = Range.create(Position.create(0, 0), document.positionAt(value.length));
}
return { value, range };
}