Skip to content

Commit

Permalink
feat: VitePress support (#1399)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jun 6, 2022
1 parent 1630f5a commit 33c9c1c
Show file tree
Hide file tree
Showing 33 changed files with 775 additions and 220 deletions.
@@ -0,0 +1,55 @@
{
"autoClosingPairs": [
// html
{
"open": "{",
"close": "}"
},
{
"open": "[",
"close": "]"
},
{
"open": "(",
"close": ")"
},
{
"open": "'",
"close": "'"
},
{
"open": "\"",
"close": "\""
},
{
"open": "<!--",
"close": "-->",
"notIn": [
"comment",
"string"
]
},
// javascript
{
"open": "`",
"close": "`",
"notIn": [
"string",
"comment"
]
},
{
"open": "/**",
"close": " */",
"notIn": [
"string"
]
}
],
"colorizedBracketPairs": [
[
"{{",
"}}"
],
]
}
42 changes: 42 additions & 0 deletions extensions/vscode-vue-language-features/package.json
Expand Up @@ -25,6 +25,7 @@
],
"activationEvents": [
"onLanguage:vue",
"onLanguage:markdown",
"onLanguage:javascript",
"onLanguage:typescript",
"onLanguage:javascriptreact",
Expand All @@ -45,6 +46,11 @@
}
},
"contributes": {
"configurationDefaults": {
"[markdown]": {
"editor.quickSuggestions": true
}
},
"jsonValidation": [
{
"fileMatch": "tsconfig.json",
Expand Down Expand Up @@ -119,6 +125,10 @@
],
"configuration": "./languages/vue-language-configuration.json"
},
{
"id": "markdown",
"configuration": "./languages/markdown-language-configuration.json"
},
{
"id": "html",
"configuration": "./languages/sfc-template-language-configuration.json"
Expand Down Expand Up @@ -450,6 +460,38 @@
"support.class.component.vue"
]
}
},
{
"language": "markdown",
"scopes": {
"property": [
"variable.other.property.vue"
],
"property.readonly": [
"variable.other.constant.property.vue"
],
"variable": [
"variable.other.readwrite.vue"
],
"variable.readonly": [
"variable.other.constant.object.vue"
],
"function": [
"entity.name.function.vue"
],
"namespace": [
"entity.name.type.module.vue"
],
"variable.defaultLibrary": [
"support.variable.vue"
],
"function.defaultLibrary": [
"support.function.vue"
],
"componentTag": [
"support.class.component.vue"
]
}
}
],
"commands": [
Expand Down
Expand Up @@ -17,7 +17,7 @@ export function activate(context: vscode.ExtensionContext) {
initializationOptions: initOptions,
progressOnInitialization: true,
synchronize: {
fileEvents: vscode.workspace.createFileSystemWatcher('{**/*.vue,**/*.js,**/*.jsx,**/*.ts,**/*.tsx,**/*.json}')
fileEvents: vscode.workspace.createFileSystemWatcher('{**/*.vue,**/*.md,**/*.js,**/*.jsx,**/*.ts,**/*.tsx,**/*.json}')
}
};
const client = new lsp.LanguageClient(
Expand Down
6 changes: 5 additions & 1 deletion extensions/vscode-vue-language-features/src/common.ts
Expand Up @@ -45,7 +45,7 @@ export async function activate(context: vscode.ExtensionContext, createLc: Creat
}

const currentlangId = vscode.window.activeTextEditor.document.languageId;
if (currentlangId === 'vue') {
if (currentlangId === 'vue' || currentlangId === 'markdown') {
doActivate(context, createLc);
stopCheck.dispose();
}
Expand All @@ -66,23 +66,27 @@ async function doActivate(context: vscode.ExtensionContext, createLc: CreateLang
const languageFeaturesDocumentSelector: lsp.DocumentSelector = takeOverMode ?
[
{ scheme: 'file', language: 'vue' },
{ scheme: 'file', language: 'markdown' },
{ scheme: 'file', language: 'javascript' },
{ scheme: 'file', language: 'typescript' },
{ scheme: 'file', language: 'javascriptreact' },
{ scheme: 'file', language: 'typescriptreact' },
{ scheme: 'file', language: 'json' },
] : [
{ scheme: 'file', language: 'vue' },
{ scheme: 'file', language: 'markdown' },
];
const documentFeaturesDocumentSelector: lsp.DocumentSelector = takeOverMode ?
[
{ language: 'vue' },
{ language: 'markdown' },
{ language: 'javascript' },
{ language: 'typescript' },
{ language: 'javascriptreact' },
{ language: 'typescriptreact' },
] : [
{ language: 'vue' },
{ language: 'markdown' },
];
const _useSecondServer = useSecondServer();
const _serverMaxOldSpaceSize = serverMaxOldSpaceSize();
Expand Down
Expand Up @@ -67,7 +67,7 @@ export async function activate(context: vscode.ExtensionContext, languageClient:
};

async function onChangeDocument(newDoc: vscode.TextDocument | undefined) {
if (newDoc?.languageId === 'vue') {
if (newDoc?.languageId === 'vue' || newDoc?.languageId === 'markdown') {
let attrCase = attrCases.uriGet(newDoc.uri.toString());
if (!attrCase) {
const attrMode = vscode.workspace.getConfiguration('volar').get<'auto-kebab' | 'auto-camel' | 'kebab' | 'camel'>('completion.preferredAttrNameCase');
Expand Down
Expand Up @@ -6,6 +6,7 @@ export async function activate(context: vscode.ExtensionContext, htmlClient: Bas

const supportedLanguages: Record<string, boolean> = {
vue: true,
markdown: true,
javascript: true,
typescript: true,
javascriptreact: true,
Expand Down
Expand Up @@ -97,7 +97,7 @@ export async function activate(context: vscode.ExtensionContext, languageClient:
};

async function onChangeDocument(newDoc: vscode.TextDocument | undefined) {
if (newDoc?.languageId === 'vue') {
if (newDoc?.languageId === 'vue' || newDoc?.languageId === 'markdown') {
let tagCase = tagCases.uriGet(newDoc.uri.toString());
if (!tagCase) {
const tagMode = vscode.workspace.getConfiguration('volar').get<'auto' | 'both' | 'kebab' | 'pascal'>('completion.preferredTagNameCase');
Expand Down
Expand Up @@ -87,6 +87,7 @@ export async function activate(context: vscode.ExtensionContext, clients: BaseLa
function updateStatusBar() {
if (
vscode.window.activeTextEditor?.document.languageId !== 'vue'
&& vscode.window.activeTextEditor?.document.languageId !== 'markdown'
&& !(
takeOverModeEnabled()
&& vscode.window.activeTextEditor
Expand Down
Expand Up @@ -20,6 +20,7 @@ export async function activate(context: vscode.ExtensionContext, languageClient:
async function updateStatusBar() {
if (
vscode.window.activeTextEditor?.document.languageId !== 'vue'
&& vscode.window.activeTextEditor?.document.languageId !== 'markdown'
&& !(
takeOverModeEnabled()
&& vscode.window.activeTextEditor
Expand Down
Expand Up @@ -35,7 +35,7 @@ export function activate(context: vscode.ExtensionContext) {
initializationOptions: initOptions,
progressOnInitialization: true,
synchronize: {
fileEvents: vscode.workspace.createFileSystemWatcher('{**/*.vue,**/*.js,**/*.jsx,**/*.ts,**/*.tsx,**/*.json}')
fileEvents: vscode.workspace.createFileSystemWatcher('{**/*.vue,**/*.md,**/*.js,**/*.jsx,**/*.ts,**/*.tsx,**/*.json}')
}
};
const client = new lsp.LanguageClient(
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/common.ts
Expand Up @@ -13,6 +13,7 @@ export function syntaxToLanguageId(syntax: string) {
case 'jsx': return 'javascriptreact';
case 'tsx': return 'typescriptreact';
case 'pug': return 'jade';
case 'md': return 'markdown';
}
return syntax;
}
Expand All @@ -24,6 +25,7 @@ export function languageIdToSyntax(languageId: string) {
case 'javascriptreact': return 'jsx';
case 'typescriptreact': return 'tsx';
case 'jade': return 'pug';
case 'markdown': return 'md';
}
return languageId;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/typescript-vue-plugin/src/index.ts
Expand Up @@ -19,6 +19,7 @@ const init: ts.server.PluginModuleFactory = (modules) => {
info.project.getScriptKind = fileName => {
switch (path.extname(fileName)) {
case '.vue': return ts.ScriptKind.TSX; // can't use External, Unknown
case '.md': return ts.ScriptKind.TSX; // can't use External, Unknown
case '.js': return ts.ScriptKind.JS;
case '.jsx': return ts.ScriptKind.JSX;
case '.ts': return ts.ScriptKind.TS;
Expand Down Expand Up @@ -158,7 +159,7 @@ function createProxyHost(ts: typeof import('typescript/lib/tsserverlibrary'), in
};

async function onAnyDriveFileUpdated(fileName: string) {
if (fileName.endsWith('.vue') && info.project.fileExists(fileName) && !vueFiles.has(fileName)) {
if ((fileName.endsWith('.vue') || fileName.endsWith('.md')) && info.project.fileExists(fileName) && !vueFiles.has(fileName)) {
onConfigUpdated();
}
}
Expand Down Expand Up @@ -202,7 +203,7 @@ function createProxyHost(ts: typeof import('typescript/lib/tsserverlibrary'), in
const parseConfigHost: ts.ParseConfigHost = {
useCaseSensitiveFileNames: info.project.useCaseSensitiveFileNames(),
readDirectory: (path, extensions, exclude, include, depth) => {
return info.project.readDirectory(path, ['.vue'], exclude, include, depth);
return info.project.readDirectory(path, ['.vue', '.md'], exclude, include, depth);
},
fileExists: fileName => info.project.fileExists(fileName),
readFile: fileName => info.project.readFile(fileName),
Expand Down
10 changes: 5 additions & 5 deletions packages/vue-code-gen/src/generators/template.ts
Expand Up @@ -65,7 +65,7 @@ export function generate(
allowTypeNarrowingInEventExpressions: boolean,
hasScriptSetup: boolean,
cssScopedClasses: string[] = [],
htmlToTemplate: (htmlStart: number, htmlEnd: number) => { start: number, end: number; } | undefined,
htmlToTemplate: (htmlRange: { start: number, end: number; }) => { start: number, end: number; } | undefined,
searchTexts: {
getEmitCompletion(tag: string): string,
getPropsCompletion(tag: string): string,
Expand Down Expand Up @@ -228,7 +228,7 @@ export function generate(
tagResolves[tagName] = {
component: var_rawComponent,
emit: var_emit,
offsets: tagOffsets.map(offset => htmlToTemplate(offset, offset)?.start).filter(notEmpty),
offsets: tagOffsets.map(offset => htmlToTemplate({ start: offset, end: offset })?.start).filter(notEmpty),
};
}

Expand Down Expand Up @@ -1182,7 +1182,7 @@ export function generate(
end: prop.arg.loc.start.offset + end,
};

const newStart = htmlToTemplate(sourceRange.start, sourceRange.end)?.start;
const newStart = htmlToTemplate({ start: sourceRange.start, end: sourceRange.end })?.start;
if (newStart === undefined) continue;
const offset = newStart - sourceRange.start;
sourceRange.start += offset;
Expand Down Expand Up @@ -1847,7 +1847,7 @@ export function generate(
function addMapping(gen: typeof tsCodeGen, mapping: SourceMaps.Mapping<EmbeddedFileMappingData>) {
const newMapping = { ...mapping };

const templateStart = htmlToTemplate(mapping.sourceRange.start, mapping.sourceRange.end)?.start;
const templateStart = htmlToTemplate(mapping.sourceRange)?.start;
if (templateStart === undefined) return; // not found
const offset = templateStart - mapping.sourceRange.start;
newMapping.sourceRange = {
Expand All @@ -1858,7 +1858,7 @@ export function generate(
if (mapping.additional) {
newMapping.additional = [];
for (const other of mapping.additional) {
let otherTemplateStart = htmlToTemplate(other.sourceRange.start, other.sourceRange.end)?.start;
let otherTemplateStart = htmlToTemplate(other.sourceRange)?.start;
if (otherTemplateStart === undefined) continue;
const otherOffset = otherTemplateStart - other.sourceRange.start;
newMapping.additional.push({
Expand Down
Expand Up @@ -279,7 +279,7 @@ export function register(
});
connection.workspace.onWillRenameFiles(async handler => {

const hasTsFile = handler.files.some(file => file.newUri.endsWith('.vue') || file.newUri.endsWith('.ts') || file.newUri.endsWith('.tsx'));
const hasTsFile = handler.files.some(file => file.newUri.endsWith('.vue') || file.newUri.endsWith('.md') || file.newUri.endsWith('.ts') || file.newUri.endsWith('.tsx'));
const config: 'prompt' | 'always' | 'never' | null | undefined = await connection.workspace.getConfiguration(hasTsFile ? 'typescript.updateImportsOnFileMove.enabled' : 'javascript.updateImportsOnFileMove.enabled');

if (config === 'always') {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-language-server/src/project.ts
Expand Up @@ -243,7 +243,7 @@ export async function createProject(
const parseConfigHost: ts.ParseConfigHost = {
useCaseSensitiveFileNames: projectSys.useCaseSensitiveFileNames,
readDirectory: (path, extensions, exclude, include, depth) => {
return projectSys.readDirectory(path, [...extensions, '.vue'], exclude, include, depth);
return projectSys.readDirectory(path, [...extensions, '.vue', '.md'], exclude, include, depth);
},
fileExists: projectSys.fileExists,
readFile: projectSys.readFile,
Expand Down
Expand Up @@ -36,6 +36,7 @@ export function register(
willRename: {
filters: [
{ pattern: { glob: '**/*.vue' } },
{ pattern: { glob: '**/*.md' } },
{ pattern: { glob: '**/*.js' } },
{ pattern: { glob: '**/*.ts' } },
{ pattern: { glob: '**/*.jsx' } },
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-language-service/src/documentService.ts
Expand Up @@ -116,7 +116,7 @@ export function getDocumentService(

function getVueDocument(document: TextDocument) {

if (document.languageId !== 'vue')
if (document.languageId !== 'vue' && document.languageId !== 'markdown')
return;

let vueDoc = vueDocuments.get(document);
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-language-service/src/languageService.ts
Expand Up @@ -327,7 +327,7 @@ export function createLanguageService(

document = TextDocument.create(
uri,
uri.endsWith('.vue') ? 'vue' : 'typescript', // TODO
shared.syntaxToLanguageId(upath.extname(uri).slice(1)),
newVersion,
scriptSnapshot.getText(0, scriptSnapshot.getLength()),
);
Expand Down
Expand Up @@ -95,7 +95,7 @@ export default function (options: {
function worker<T>(uri: string, callback: (vueDocument: VueDocument) => T) {

const vueDocument = options.getVueDocument(uri);
if (!vueDocument)
if (!vueDocument || vueDocument.file.fileName.endsWith('.md'))
return;

return callback(vueDocument);
Expand Down

0 comments on commit 33c9c1c

Please sign in to comment.