0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

[ci] yarn format

This commit is contained in:
matthewp 2021-08-10 13:31:09 +00:00 committed by GitHub Actions
parent 2c5380a266
commit 16790aee7b
10 changed files with 280 additions and 340 deletions

View file

@ -67,8 +67,8 @@ export function startServer() {
hoverProvider: true, hoverProvider: true,
signatureHelpProvider: { signatureHelpProvider: {
triggerCharacters: ['(', ',', '<'], triggerCharacters: ['(', ',', '<'],
retriggerCharacters: [')'] retriggerCharacters: [')'],
} },
}, },
}; };
}); });

View file

@ -7,7 +7,7 @@ import type {
Position, Position,
SignatureHelp, SignatureHelp,
SignatureHelpContext, SignatureHelpContext,
TextDocumentIdentifier TextDocumentIdentifier,
} from 'vscode-languageserver'; } from 'vscode-languageserver';
import type { DocumentManager } from '../core/documents'; import type { DocumentManager } from '../core/documents';
import type * as d from './interfaces'; import type * as d from './interfaces';
@ -126,11 +126,7 @@ export class PluginHost {
throw new Error('Cannot call methods on an unopened document'); throw new Error('Cannot call methods on an unopened document');
} }
return await this.execute<any>( return await this.execute<any>('getSignatureHelp', [document, position, context, cancellationToken], ExecuteMode.FirstNonNull);
'getSignatureHelp',
[document, position, context, cancellationToken],
ExecuteMode.FirstNonNull
);
} }
onWatchFileChanges(onWatchFileChangesParams: any[]): void { onWatchFileChanges(onWatchFileChangesParams: any[]): void {
@ -159,10 +155,12 @@ export class PluginHost {
} }
return null; return null;
case ExecuteMode.Collect: case ExecuteMode.Collect:
return Promise.all(plugins.map((plugin) => { return Promise.all(
plugins.map((plugin) => {
let ret = this.tryExecutePlugin(plugin, name, args, []); let ret = this.tryExecutePlugin(plugin, name, args, []);
return ret; return ret;
})); })
);
case ExecuteMode.None: case ExecuteMode.None:
await Promise.all(plugins.map((plugin) => this.tryExecutePlugin(plugin, name, args, null))); await Promise.all(plugins.map((plugin) => this.tryExecutePlugin(plugin, name, args, null)));
return; return;

View file

@ -75,9 +75,11 @@ class AstroDocumentSnapshot implements DocumentSnapshot {
/** @internal */ /** @internal */
private transformContent(content: string) { private transformContent(content: string) {
return content.replace(/---/g, '///') + return (
content.replace(/---/g, '///') +
// Add TypeScript definitions // Add TypeScript definitions
ASTRO_DEFINITION; ASTRO_DEFINITION
);
} }
get filePath() { get filePath() {
@ -139,9 +141,11 @@ export class DocumentFragmentSnapshot implements Omit<DocumentSnapshot, 'getFrag
/** @internal */ /** @internal */
private transformContent(content: string) { private transformContent(content: string) {
return content.replace(/---/g, '///') + return (
content.replace(/---/g, '///') +
// Add TypeScript definitions // Add TypeScript definitions
ASTRO_DEFINITION; ASTRO_DEFINITION
);
} }
getText(start: number, end: number) { getText(start: number, end: number) {

View file

@ -1,11 +1,6 @@
import type { ConfigManager } from '../../core/config'; import type { ConfigManager } from '../../core/config';
import type { CompletionsProvider, AppCompletionItem, AppCompletionList } from '../interfaces'; import type { CompletionsProvider, AppCompletionItem, AppCompletionList } from '../interfaces';
import type { import type { CancellationToken, Hover, SignatureHelp, SignatureHelpContext } from 'vscode-languageserver';
CancellationToken,
Hover,
SignatureHelp,
SignatureHelpContext
} from 'vscode-languageserver';
import { join as pathJoin, dirname as pathDirname } from 'path'; import { join as pathJoin, dirname as pathDirname } from 'path';
import { Document, DocumentManager, isInsideFrontmatter } from '../../core/documents'; import { Document, DocumentManager, isInsideFrontmatter } from '../../core/documents';
import { SourceFile, ImportDeclaration, Node, SyntaxKind } from 'typescript'; import { SourceFile, ImportDeclaration, Node, SyntaxKind } from 'typescript';
@ -134,18 +129,8 @@ export class TypeScriptPlugin implements CompletionsProvider {
} }
} }
async getSignatureHelp( async getSignatureHelp(document: Document, position: Position, context: SignatureHelpContext | undefined, cancellationToken?: CancellationToken): Promise<SignatureHelp | null> {
document: Document, return this.signatureHelpProvider.getSignatureHelp(document, position, context, cancellationToken);
position: Position,
context: SignatureHelpContext | undefined,
cancellationToken?: CancellationToken
): Promise<SignatureHelp | null> {
return this.signatureHelpProvider.getSignatureHelp(
document,
position,
context,
cancellationToken
);
} }
/** /**

View file

@ -34,8 +34,7 @@ export class CompletionsProviderImpl implements CompletionsProvider<CompletionEn
const offset = document.offsetAt(position); const offset = document.offsetAt(position);
const entries = const entries = lang.getCompletionsAtPosition(fragment.filePath, offset, completionOptions)?.entries || [];
lang.getCompletionsAtPosition(fragment.filePath, offset, completionOptions)?.entries || [];
const completionItems = entries const completionItems = entries
.map((entry: ts.CompletionEntry) => this.toCompletionItem(fragment, entry, document.uri, position, new Set())) .map((entry: ts.CompletionEntry) => this.toCompletionItem(fragment, entry, document.uri, position, new Set()))

View file

@ -26,13 +26,11 @@ export class HoverProviderImpl implements HoverProvider {
const documentation = getMarkdownDocumentation(info.documentation, info.tags); const documentation = getMarkdownDocumentation(info.documentation, info.tags);
// https://microsoft.github.io/language-server-protocol/specification#textDocument_hover // https://microsoft.github.io/language-server-protocol/specification#textDocument_hover
const contents = ['```typescript', declaration, '```'] const contents = ['```typescript', declaration, '```'].concat(documentation ? ['---', documentation] : []).join('\n');
.concat(documentation ? ['---', documentation] : [])
.join('\n');
return mapObjWithRangeToOriginal(fragment, { return mapObjWithRangeToOriginal(fragment, {
range: convertRange(fragment, textSpan), range: convertRange(fragment, textSpan),
contents contents,
}); });
} }

View file

@ -9,7 +9,7 @@ import {
SignatureInformation, SignatureInformation,
ParameterInformation, ParameterInformation,
MarkupKind, MarkupKind,
CancellationToken CancellationToken,
} from 'vscode-languageserver'; } from 'vscode-languageserver';
import { Document } from '../../../core/documents'; import { Document } from '../../../core/documents';
import { getMarkdownDocumentation } from '../previewer'; import { getMarkdownDocumentation } from '../previewer';
@ -21,12 +21,7 @@ export class SignatureHelpProviderImpl implements SignatureHelpProvider {
private static readonly triggerCharacters = ['(', ',', '<']; private static readonly triggerCharacters = ['(', ',', '<'];
private static readonly retriggerCharacters = [')']; private static readonly retriggerCharacters = [')'];
async getSignatureHelp( async getSignatureHelp(document: Document, position: Position, context: SignatureHelpContext | undefined, cancellationToken?: CancellationToken): Promise<SignatureHelp | null> {
document: Document,
position: Position,
context: SignatureHelpContext | undefined,
cancellationToken?: CancellationToken
): Promise<SignatureHelp | null> {
const { lang, tsDoc } = await this.lang.getTypeScriptDoc(document); const { lang, tsDoc } = await this.lang.getTypeScriptDoc(document);
const fragment = await tsDoc.getFragment(); const fragment = await tsDoc.getFragment();
@ -36,15 +31,8 @@ export class SignatureHelpProviderImpl implements SignatureHelpProvider {
const offset = fragment.offsetAt(fragment.getGeneratedPosition(position)); const offset = fragment.offsetAt(fragment.getGeneratedPosition(position));
const triggerReason = this.toTsTriggerReason(context); const triggerReason = this.toTsTriggerReason(context);
const info = lang.getSignatureHelpItems( const info = lang.getSignatureHelpItems(toVirtualAstroFilePath(tsDoc.filePath), offset, triggerReason ? { triggerReason } : undefined);
toVirtualAstroFilePath(tsDoc.filePath), if (!info || info.items.some((signature) => this.isInSvelte2tsxGeneratedFunction(signature))) {
offset,
triggerReason ? { triggerReason } : undefined
);
if (
!info ||
info.items.some((signature) => this.isInSvelte2tsxGeneratedFunction(signature))
) {
return null; return null;
} }
@ -53,33 +41,22 @@ export class SignatureHelpProviderImpl implements SignatureHelpProvider {
return { return {
signatures, signatures,
activeSignature: info.selectedItemIndex, activeSignature: info.selectedItemIndex,
activeParameter: info.argumentIndex activeParameter: info.argumentIndex,
}; };
} }
private isReTrigger( private isReTrigger(isRetrigger: boolean, triggerCharacter: string): triggerCharacter is ts.SignatureHelpRetriggerCharacter {
isRetrigger: boolean, return isRetrigger && (this.isTriggerCharacter(triggerCharacter) || SignatureHelpProviderImpl.retriggerCharacters.includes(triggerCharacter));
triggerCharacter: string
): triggerCharacter is ts.SignatureHelpRetriggerCharacter {
return (
isRetrigger &&
(this.isTriggerCharacter(triggerCharacter) ||
SignatureHelpProviderImpl.retriggerCharacters.includes(triggerCharacter))
);
} }
private isTriggerCharacter( private isTriggerCharacter(triggerCharacter: string): triggerCharacter is ts.SignatureHelpTriggerCharacter {
triggerCharacter: string
): triggerCharacter is ts.SignatureHelpTriggerCharacter {
return SignatureHelpProviderImpl.triggerCharacters.includes(triggerCharacter); return SignatureHelpProviderImpl.triggerCharacters.includes(triggerCharacter);
} }
/** /**
* adopted from https://github.com/microsoft/vscode/blob/265a2f6424dfbd3a9788652c7d376a7991d049a3/extensions/typescript-language-features/src/languageFeatures/signatureHelp.ts#L103 * adopted from https://github.com/microsoft/vscode/blob/265a2f6424dfbd3a9788652c7d376a7991d049a3/extensions/typescript-language-features/src/languageFeatures/signatureHelp.ts#L103
*/ */
private toTsTriggerReason( private toTsTriggerReason(context: SignatureHelpContext | undefined): ts.SignatureHelpTriggerReason {
context: SignatureHelpContext | undefined
): ts.SignatureHelpTriggerReason {
switch (context?.triggerKind) { switch (context?.triggerKind) {
case SignatureHelpTriggerKind.TriggerCharacter: case SignatureHelpTriggerKind.TriggerCharacter:
if (context.triggerCharacter) { if (context.triggerCharacter) {
@ -89,7 +66,7 @@ export class SignatureHelpProviderImpl implements SignatureHelpProvider {
if (this.isTriggerCharacter(context.triggerCharacter)) { if (this.isTriggerCharacter(context.triggerCharacter)) {
return { return {
kind: 'characterTyped', kind: 'characterTyped',
triggerCharacter: context.triggerCharacter triggerCharacter: context.triggerCharacter,
}; };
} }
} }
@ -107,11 +84,7 @@ export class SignatureHelpProviderImpl implements SignatureHelpProvider {
* adopted from https://github.com/microsoft/vscode/blob/265a2f6424dfbd3a9788652c7d376a7991d049a3/extensions/typescript-language-features/src/languageFeatures/signatureHelp.ts#L73 * adopted from https://github.com/microsoft/vscode/blob/265a2f6424dfbd3a9788652c7d376a7991d049a3/extensions/typescript-language-features/src/languageFeatures/signatureHelp.ts#L73
*/ */
private toSignatureHelpInformation(item: ts.SignatureHelpItem): SignatureInformation { private toSignatureHelpInformation(item: ts.SignatureHelpItem): SignatureInformation {
const [prefixLabel, separatorLabel, suffixLabel] = [ const [prefixLabel, separatorLabel, suffixLabel] = [item.prefixDisplayParts, item.separatorDisplayParts, item.suffixDisplayParts].map(ts.displayPartsToString);
item.prefixDisplayParts,
item.separatorDisplayParts,
item.suffixDisplayParts
].map(ts.displayPartsToString);
let textIndex = prefixLabel.length; let textIndex = prefixLabel.length;
let signatureLabel = ''; let signatureLabel = '';
@ -143,16 +116,14 @@ export class SignatureHelpProviderImpl implements SignatureHelpProvider {
documentation: signatureDocumentation documentation: signatureDocumentation
? { ? {
value: signatureDocumentation, value: signatureDocumentation,
kind: MarkupKind.Markdown kind: MarkupKind.Markdown,
} }
: undefined, : undefined,
parameters parameters,
}; };
} }
private isInSvelte2tsxGeneratedFunction(signatureHelpItem: ts.SignatureHelpItem) { private isInSvelte2tsxGeneratedFunction(signatureHelpItem: ts.SignatureHelpItem) {
return signatureHelpItem.prefixDisplayParts.some((part) => return signatureHelpItem.prefixDisplayParts.some((part) => part.text.includes('__sveltets'));
part.text.includes('__sveltets')
);
} }
} }

View file

@ -14,9 +14,7 @@
return ( return (
text text
// Http(s) links // Http(s) links
.replace( .replace(/\{@(link|linkplain|linkcode) (https?:\/\/[^ |}]+?)(?:[| ]([^{}\n]+?))?\}/gi, (_, tag: string, link: string, text?: string) => {
/\{@(link|linkplain|linkcode) (https?:\/\/[^ |}]+?)(?:[| ]([^{}\n]+?))?\}/gi,
(_, tag: string, link: string, text?: string) => {
switch (tag) { switch (tag) {
case 'linkcode': case 'linkcode':
return `[\`${text ? text.trim() : link}\`](${link})`; return `[\`${text ? text.trim() : link}\`](${link})`;
@ -24,8 +22,7 @@
default: default:
return `[${text ? text.trim() : link}](${link})`; return `[${text ? text.trim() : link}](${link})`;
} }
} })
)
); );
} }
@ -50,11 +47,7 @@
// check for caption tags, fix for https://github.com/microsoft/vscode/issues/79704 // check for caption tags, fix for https://github.com/microsoft/vscode/issues/79704
const captionTagMatches = text.match(/<caption>(.*?)<\/caption>\s*(\r\n|\n)/); const captionTagMatches = text.match(/<caption>(.*?)<\/caption>\s*(\r\n|\n)/);
if (captionTagMatches && captionTagMatches.index === 0) { if (captionTagMatches && captionTagMatches.index === 0) {
return ( return captionTagMatches[1] + '\n\n' + makeCodeblock(text.substr(captionTagMatches[0].length));
captionTagMatches[1] +
'\n\n' +
makeCodeblock(text.substr(captionTagMatches[0].length))
);
} else { } else {
return makeCodeblock(text); return makeCodeblock(text);
} }
@ -93,12 +86,7 @@
if (!doc) { if (!doc) {
return label; return label;
} }
return ( return label + (doc.match(/\r\n|\n/g) ? ' \n' + processInlineTags(doc) : `${processInlineTags(doc)}`);
label +
(doc.match(/\r\n|\n/g)
? ' \n' + processInlineTags(doc)
: `${processInlineTags(doc)}`)
);
} }
} }
@ -123,10 +111,7 @@
return processInlineTags(typeof parts === 'string' ? parts : ts.displayPartsToString(parts)); return processInlineTags(typeof parts === 'string' ? parts : ts.displayPartsToString(parts));
} }
export function getMarkdownDocumentation( export function getMarkdownDocumentation(documentation: ts.SymbolDisplayPart[] | undefined, tags: ts.JSDocTagInfo[] | undefined) {
documentation: ts.SymbolDisplayPart[] | undefined,
tags: ts.JSDocTagInfo[] | undefined
) {
let result: Array<string | undefined> = []; let result: Array<string | undefined> = [];
if (documentation) { if (documentation) {
result.push(plain(documentation)); result.push(plain(documentation));