Skip to content

Commit

Permalink
Merge pull request #89 from microsoft/dev/mjbvz/private-hash
Browse files Browse the repository at this point in the history
Use ES # privates instead of private keyword
  • Loading branch information
mjbvz authored Nov 2, 2022
2 parents 1722643 + a98f300 commit 70ce291
Show file tree
Hide file tree
Showing 28 changed files with 749 additions and 615 deletions.
16 changes: 4 additions & 12 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@ module.exports = {
'no-useless-escape': 0,
'semi': 1,
'quotes': [1, 'single', { allowTemplateLiterals: true }],
'@typescript-eslint/naming-convention': [
'no-restricted-syntax': [
'warn',
{
'selector': 'default',
'modifiers': ['private'],
'format': null,
'leadingUnderscore': 'require'
selector: ':matches(PropertyDefinition, TSParameterProperty, MethodDefinition[key.name!="constructor"])[accessibility="private"]',
message: 'Use #private instead',
},
{
'selector': 'default',
'modifiers': ['public'],
'format': null,
'leadingUnderscore': 'forbid'
}
],
},
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"api-extractor": "npx api-extractor run --local",
"compile": "tsc -b tsconfig.json",
"watch": "tsc -b tsconfig.json --watch",
"lint": "eslint src/**/*.ts",
"lint": "eslint \"src/**/*.ts\"",
"prepublishOnly": "npm run compile && npm run api-extractor",
"test": "mocha 'out/test/**/*.test.js' --ui=tdd --timeout=2000 --exit"
},
Expand Down
38 changes: 20 additions & 18 deletions src/languageFeatures/codeActions/extractLinkDef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,36 @@ export class MdExtractLinkDefinitionCodeActionProvider {

public static readonly genericTitle = localize('genericTitle', 'Extract to link definition');

private static _kind = lsp.CodeActionKind.RefactorExtract + '.linkDefinition';
static #kind = lsp.CodeActionKind.RefactorExtract + '.linkDefinition';

public static readonly notOnLinkAction: lsp.CodeAction = {
title: MdExtractLinkDefinitionCodeActionProvider.genericTitle,
kind: MdExtractLinkDefinitionCodeActionProvider._kind,
kind: MdExtractLinkDefinitionCodeActionProvider.#kind,
disabled: {
reason: localize('disabled.notOnLink', 'Not on link'),
}
};

public static readonly alreadyRefLinkAction: lsp.CodeAction = {
title: MdExtractLinkDefinitionCodeActionProvider.genericTitle,
kind: MdExtractLinkDefinitionCodeActionProvider._kind,
kind: MdExtractLinkDefinitionCodeActionProvider.#kind,
disabled: {
reason: localize('disabled.alreadyRefLink', 'Link is already a reference'),
}
};

constructor(
private readonly _linkProvider: MdLinkProvider
) { }
readonly #linkProvider: MdLinkProvider;

constructor(linkProvider: MdLinkProvider) {
this.#linkProvider = linkProvider;
}

async getActions(doc: ITextDocument, range: lsp.Range, context: lsp.CodeActionContext, token: CancellationToken): Promise<lsp.CodeAction[]> {
if (!this._isEnabled(context)) {
if (!this.#isEnabled(context)) {
return [];
}

const linkInfo = await this._linkProvider.getLinks(doc);
const linkInfo = await this.#linkProvider.getLinks(doc);
if (token.isCancellationRequested) {
return [];
}
Expand All @@ -67,31 +69,31 @@ export class MdExtractLinkDefinitionCodeActionProvider {
return [MdExtractLinkDefinitionCodeActionProvider.alreadyRefLinkAction];
}

return [this._getExtractLinkAction(doc, linkInfo, targetLink as MdInlineLink<InternalHref | ExternalHref>)];
return [this.#getExtractLinkAction(doc, linkInfo, targetLink as MdInlineLink<InternalHref | ExternalHref>)];
}

private _isEnabled(context: lsp.CodeActionContext): boolean {
#isEnabled(context: lsp.CodeActionContext): boolean {
if (typeof context.only === 'undefined') {
return true;
}

return context.only.some(kind => codeActionKindContains(lsp.CodeActionKind.Refactor, kind));
}

private _getExtractLinkAction(doc: ITextDocument, linkInfo: MdDocumentLinksInfo, targetLink: MdInlineLink<InternalHref | ExternalHref>): lsp.CodeAction {
#getExtractLinkAction(doc: ITextDocument, linkInfo: MdDocumentLinksInfo, targetLink: MdInlineLink<InternalHref | ExternalHref>): lsp.CodeAction {
const builder = new WorkspaceEditBuilder();
const resource = getDocUri(doc);
const placeholder = this._getPlaceholder(linkInfo.definitions);
const placeholder = this.#getPlaceholder(linkInfo.definitions);

// Rewrite all inline occurrences of the link
for (const link of linkInfo.links) {
if (link.kind === MdLinkKind.Link && this._matchesHref(targetLink.href, link)) {
if (link.kind === MdLinkKind.Link && this.#matchesHref(targetLink.href, link)) {
builder.replace(resource, link.source.targetRange, `[${placeholder}]`);
}
}

// And append new definition to link definition block
const definitionText = this._getLinkTargetText(doc, targetLink).trim();
const definitionText = this.#getLinkTargetText(doc, targetLink).trim();
const definitions = linkInfo.links.filter(link => link.kind === MdLinkKind.Definition) as MdLinkDefinition[];
const defBlock = getExistingDefinitionBlock(doc, definitions);
if (!defBlock) {
Expand All @@ -104,7 +106,7 @@ export class MdExtractLinkDefinitionCodeActionProvider {
const renamePosition = translatePosition(targetLink.source.targetRange.start, { characterDelta: 1 });
return {
title: MdExtractLinkDefinitionCodeActionProvider.genericTitle,
kind: MdExtractLinkDefinitionCodeActionProvider._kind,
kind: MdExtractLinkDefinitionCodeActionProvider.#kind,
edit: builder.renameFragment(),
command: {
command: 'vscodeMarkdownLanguageservice.rename',
Expand All @@ -114,14 +116,14 @@ export class MdExtractLinkDefinitionCodeActionProvider {
};
}

private _getLinkTargetText(doc: ITextDocument, link: MdInlineLink) {
#getLinkTargetText(doc: ITextDocument, link: MdInlineLink) {
const afterHrefRange = makeRange(
translatePosition(link.source.targetRange.start, { characterDelta: 1 }),
translatePosition(link.source.targetRange.end, { characterDelta: -1 }));
return doc.getText(afterHrefRange);
}

private _getPlaceholder(definitions: LinkDefinitionSet): string {
#getPlaceholder(definitions: LinkDefinitionSet): string {
const base = 'def';
for (let i = 1; ; ++i) {
const name = i === 1 ? base : `${base}${i}`;
Expand All @@ -131,7 +133,7 @@ export class MdExtractLinkDefinitionCodeActionProvider {
}
}

private _matchesHref(href: InternalHref | ExternalHref, link: MdLink): boolean {
#matchesHref(href: InternalHref | ExternalHref, link: MdLink): boolean {
if (link.href.kind === HrefKind.External && href.kind === HrefKind.External) {
return link.href.uri.toString() === href.uri.toString();
}
Expand Down
14 changes: 7 additions & 7 deletions src/languageFeatures/codeActions/removeLinkDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ const localize = nls.loadMessageBundle();

export class MdRemoveLinkDefinitionCodeActionProvider {

private static readonly _removeUnusedDefTitle = localize('removeUnusedTitle', 'Remove unused link definition');
private static readonly _removeDuplicateDefTitle = localize('removeDuplicateTitle', 'Remove duplicate link definition');
static readonly #removeUnusedDefTitle = localize('removeUnusedTitle', 'Remove unused link definition');
static readonly #removeDuplicateDefTitle = localize('removeDuplicateTitle', 'Remove duplicate link definition');

*getActions(doc: ITextDocument, range: lsp.Range, context: lsp.CodeActionContext): Iterable<lsp.CodeAction> {
if (!this._isEnabled(context)) {
if (!this.#isEnabled(context)) {
return;
}

Expand All @@ -29,7 +29,7 @@ export class MdRemoveLinkDefinitionCodeActionProvider {
for (const diag of context.diagnostics) {
if (diag.code === DiagnosticCode.link_unusedDefinition && diag.data && rangeIntersects(diag.range, range)) {
const link = diag.data as MdLinkDefinition;
yield this._getRemoveDefinitionAction(doc, link, MdRemoveLinkDefinitionCodeActionProvider._removeUnusedDefTitle);
yield this.#getRemoveDefinitionAction(doc, link, MdRemoveLinkDefinitionCodeActionProvider.#removeUnusedDefTitle);
unusedDiagnosticLines.add(link.source.range.start.line);
}
}
Expand All @@ -38,21 +38,21 @@ export class MdRemoveLinkDefinitionCodeActionProvider {
if (diag.code === DiagnosticCode.link_duplicateDefinition && diag.data && rangeIntersects(diag.range, range)) {
const link = diag.data as MdLinkDefinition;
if (!unusedDiagnosticLines.has(link.source.range.start.line)) {
yield this._getRemoveDefinitionAction(doc, link, MdRemoveLinkDefinitionCodeActionProvider._removeDuplicateDefTitle);
yield this.#getRemoveDefinitionAction(doc, link, MdRemoveLinkDefinitionCodeActionProvider.#removeDuplicateDefTitle);
}
}
}
}

private _isEnabled(context: lsp.CodeActionContext): boolean {
#isEnabled(context: lsp.CodeActionContext): boolean {
if (typeof context.only === 'undefined') {
return true;
}

return context.only.some(kind => codeActionKindContains(lsp.CodeActionKind.QuickFix, kind));
}

private _getRemoveDefinitionAction(doc: ITextDocument, definition: MdLinkDefinition, title: string): lsp.CodeAction {
#getRemoveDefinitionAction(doc: ITextDocument, definition: MdLinkDefinition, title: string): lsp.CodeAction {
const builder = new WorkspaceEditBuilder();

const range = definition.source.range;
Expand Down
42 changes: 26 additions & 16 deletions src/languageFeatures/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@ import { HrefKind, LinkDefinitionSet, MdLink, MdLinkKind } from './documentLinks

export class MdDefinitionProvider {

readonly #configuration: LsConfiguration;
readonly #workspace: IWorkspace;
readonly #tocProvider: MdTableOfContentsProvider;
readonly #linkCache: MdWorkspaceInfoCache<readonly MdLink[]>;

constructor(
private readonly _configuration: LsConfiguration,
private readonly _workspace: IWorkspace,
private readonly _tocProvider: MdTableOfContentsProvider,
private readonly _linkCache: MdWorkspaceInfoCache<readonly MdLink[]>,
) { }
configuration: LsConfiguration,
workspace: IWorkspace,
tocProvider: MdTableOfContentsProvider,
linkCache: MdWorkspaceInfoCache<readonly MdLink[]>,
) {
this.#configuration = configuration;
this.#workspace = workspace;
this.#tocProvider = tocProvider;
this.#linkCache = linkCache;
}

async provideDefinition(document: ITextDocument, position: lsp.Position, token: CancellationToken): Promise<lsp.Definition | undefined> {
const toc = await this._tocProvider.getForDocument(document);
const toc = await this.#tocProvider.getForDocument(document);
if (token.isCancellationRequested) {
return [];
}
Expand All @@ -32,43 +42,43 @@ export class MdDefinitionProvider {
return header.headerLocation;
}

return this._getDefinitionOfLinkAtPosition(document, position, token);
return this.#getDefinitionOfLinkAtPosition(document, position, token);
}

private async _getDefinitionOfLinkAtPosition(document: ITextDocument, position: lsp.Position, token: CancellationToken): Promise<lsp.Definition | undefined> {
const docLinks = (await this._linkCache.getForDocs([document]))[0];
async #getDefinitionOfLinkAtPosition(document: ITextDocument, position: lsp.Position, token: CancellationToken): Promise<lsp.Definition | undefined> {
const docLinks = (await this.#linkCache.getForDocs([document]))[0];

for (const link of docLinks) {
if (link.kind === MdLinkKind.Definition && rangeContains(link.ref.range, position)) {
return this._getDefinitionOfRef(link.ref.text, docLinks);
return this.#getDefinitionOfRef(link.ref.text, docLinks);
}
if (rangeContains(link.source.hrefRange, position)) {
return this._getDefinitionOfLink(link, docLinks, token);
return this.#getDefinitionOfLink(link, docLinks, token);
}
}

return undefined;
}

private async _getDefinitionOfLink(sourceLink: MdLink, allLinksInFile: readonly MdLink[], token: CancellationToken): Promise<lsp.Definition | undefined> {
async #getDefinitionOfLink(sourceLink: MdLink, allLinksInFile: readonly MdLink[], token: CancellationToken): Promise<lsp.Definition | undefined> {
if (sourceLink.href.kind === HrefKind.Reference) {
return this._getDefinitionOfRef(sourceLink.href.ref, allLinksInFile);
return this.#getDefinitionOfRef(sourceLink.href.ref, allLinksInFile);
}

if (sourceLink.href.kind === HrefKind.External || !sourceLink.href.fragment) {
return undefined;
}

const resolvedResource = await statLinkToMarkdownFile(this._configuration, this._workspace, sourceLink.href.path);
const resolvedResource = await statLinkToMarkdownFile(this.#configuration, this.#workspace, sourceLink.href.path);
if (!resolvedResource || token.isCancellationRequested) {
return undefined;
}

const toc = await this._tocProvider.get(resolvedResource);
const toc = await this.#tocProvider.get(resolvedResource);
return toc.lookup(sourceLink.href.fragment)?.headerLocation;
}

private _getDefinitionOfRef(ref: string, allLinksInFile: readonly MdLink[]) {
#getDefinitionOfRef(ref: string, allLinksInFile: readonly MdLink[]) {
const allDefinitions = new LinkDefinitionSet(allLinksInFile);
const def = allDefinitions.lookup(ref);
return def ? { range: def.source.range, uri: def.source.resource.toString() } : undefined;
Expand Down
Loading

0 comments on commit 70ce291

Please sign in to comment.