Skip to content

Commit

Permalink
Pre-work for #4125
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Mar 30, 2024
1 parent c90d2f1 commit 74c3e85
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/language-service/index.ts
Expand Up @@ -30,6 +30,7 @@ import { create as createVueVisualizeHiddenCallbackParamPlugin } from './lib/plu
import { decorateLanguageServiceForVue } from '@vue/typescript-plugin/lib/common';
import { collectExtractProps } from '@vue/typescript-plugin/lib/requests/collectExtractProps';
import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from '@vue/typescript-plugin/lib/requests/componentInfos';
import { getImportPathForFile } from '@vue/typescript-plugin/lib/requests/getImportPathForFile';
import { getPropertiesAtLocation } from '@vue/typescript-plugin/lib/requests/getPropertiesAtLocation';
import { getQuickInfoAtPosition } from '@vue/typescript-plugin/lib/requests/getQuickInfoAtPosition';

Expand Down Expand Up @@ -118,6 +119,9 @@ export function createDefaultGetTsPluginClient(
async getPropertiesAtLocation(...args) {
return await getPropertiesAtLocation.apply(requestContext, args);
},
async getImportPathForFile(...args) {
return await getImportPathForFile.apply(requestContext, args);
},
async getComponentEvents(...args) {
return await getComponentEvents.apply(requestContext, args);
},
Expand Down
29 changes: 23 additions & 6 deletions packages/language-service/lib/plugins/vue-document-drop.ts
Expand Up @@ -3,15 +3,20 @@ import { camelize, capitalize, hyphenate } from '@vue/shared';
import * as path from 'path-browserify';
import type * as vscode from 'vscode-languageserver-protocol';
import { createAddComponentToOptionEdit, getLastImportNode } from '../plugins/vue-extract-file';
import { LanguageServicePlugin, LanguageServicePluginInstance, TagNameCasing } from '../types';
import { LanguageServicePlugin, LanguageServicePluginInstance, ServiceContext, TagNameCasing } from '../types';

export function create(ts: typeof import('typescript')): LanguageServicePlugin {
export function create(
ts: typeof import('typescript'),
getTsPluginClient?: (context: ServiceContext) => typeof import('@vue/typescript-plugin/lib/client') | undefined,
): LanguageServicePlugin {
return {
name: 'vue-document-drop',
create(context): LanguageServicePluginInstance {

let casing: TagNameCasing = TagNameCasing.Pascal; // TODO

const tsPluginClient = getTsPluginClient?.(context);

return {
async provideDocumentDropEdits(document, _position, dataTransfer) {

Expand Down Expand Up @@ -52,11 +57,23 @@ export function create(ts: typeof import('typescript')): LanguageServicePlugin {
const code = [...forEachEmbeddedCode(vueVirtualCode)].find(code => code.id === (sfc.scriptSetup ? 'scriptSetupFormat' : 'scriptFormat'))!;
const lastImportNode = getLastImportNode(ts, script.ast);

let importPath = path.relative(path.dirname(document.uri), importUri)
|| importUri.substring(importUri.lastIndexOf('/') + 1);
let importPath: string | undefined;

if (tsPluginClient) {
const importFileName = context.env.typescript!.uriToFileName(importUri);
const importPathRequest = await tsPluginClient.getImportPathForFile(vueVirtualCode.fileName, importFileName);
if (importPathRequest) {
importPath = importPathRequest;
}
}

if (!importPath) {
importPath = path.relative(path.dirname(document.uri), importUri)
|| importUri.substring(importUri.lastIndexOf('/') + 1);

if (!importPath.startsWith('./') && !importPath.startsWith('../')) {
importPath = './' + importPath;
if (!importPath.startsWith('./') && !importPath.startsWith('../')) {
importPath = './' + importPath;
}
}

additionalEdit.changes ??= {};
Expand Down
9 changes: 9 additions & 0 deletions packages/typescript-plugin/lib/client.ts
Expand Up @@ -10,6 +10,15 @@ export function collectExtractProps(
});
}

export async function getImportPathForFile(
...args: Parameters<typeof import('./requests/getImportPathForFile.js')['getImportPathForFile']>
) {
return await sendRequest<ReturnType<typeof import('./requests/getImportPathForFile')['getImportPathForFile']>>({
type: 'getImportPathForFile',
args,
});
}

export async function getPropertiesAtLocation(
...args: Parameters<typeof import('./requests/getPropertiesAtLocation.js')['getPropertiesAtLocation']>
) {
Expand Down
14 changes: 14 additions & 0 deletions packages/typescript-plugin/lib/requests/getImportPathForFile.ts
@@ -0,0 +1,14 @@
import type * as ts from 'typescript';

export function getImportPathForFile(
this: {
typescript: typeof import('typescript');
languageService: ts.LanguageService;
},
fileName: string,

Check failure on line 8 in packages/typescript-plugin/lib/requests/getImportPathForFile.ts

View workflow job for this annotation

GitHub Actions / build (16, ubuntu-latest)

'fileName' is declared but its value is never read.
importFileName: string,
) {
const { typescript: ts, languageService } = this;

Check failure on line 11 in packages/typescript-plugin/lib/requests/getImportPathForFile.ts

View workflow job for this annotation

GitHub Actions / build (16, ubuntu-latest)

All destructured elements are unused.
// TODO
return importFileName.replace('./', '@/');
}
6 changes: 6 additions & 0 deletions packages/typescript-plugin/lib/server.ts
Expand Up @@ -3,6 +3,7 @@ import * as net from 'net';
import type * as ts from 'typescript';
import { collectExtractProps } from './requests/collectExtractProps';
import { getComponentEvents, getComponentNames, getComponentProps, getElementAttrs, getTemplateContextProps } from './requests/componentInfos';
import { getImportPathForFile } from './requests/getImportPathForFile';
import { getPropertiesAtLocation } from './requests/getPropertiesAtLocation';
import { getQuickInfoAtPosition } from './requests/getQuickInfoAtPosition';
import { NamedPipeServer, connect, readPipeTable, updatePipeTable } from './utils';
Expand All @@ -11,6 +12,7 @@ import type { Language, VueCompilerOptions } from '@vue/language-core';
export interface Request {
type: 'containsFile'
| 'collectExtractProps'
| 'getImportPathForFile'
| 'getPropertiesAtLocation'
| 'getQuickInfoAtPosition'
// Component Infos
Expand Down Expand Up @@ -59,6 +61,10 @@ export function startNamedPipeServer(
const result = collectExtractProps.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getImportPathForFile') {
const result = getImportPathForFile.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
}
else if (request.type === 'getPropertiesAtLocation') {
const result = getPropertiesAtLocation.apply(requestContext, request.args as any);
connection.write(JSON.stringify(result ?? null));
Expand Down

0 comments on commit 74c3e85

Please sign in to comment.