Skip to content

Commit

Permalink
feat: Happy features (#288)
Browse files Browse the repository at this point in the history
* feat: ability to ignore tokens collection, fixed path-matcher for underlined files, updated test typings

* expose server config

* fix formatting
  • Loading branch information
lifeart committed May 26, 2021
1 parent 8871b54 commit fedd5c0
Show file tree
Hide file tree
Showing 13 changed files with 104 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export {
ExtensionCapabilities,
} from './utils/addon-api';
export { Project, Executor, Destructor, Linter, Watcher } from './project';
export { default as Server } from './server';
export { default as Server, IServerConfig } from './server';
23 changes: 21 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ import { CodeActionProvider } from './code-action-provider/entry';
import { log, setConsole, logError, logInfo } from './utils/logger';
import TemplateCompletionProvider from './completion-provider/template-completion-provider';
import ScriptCompletionProvider from './completion-provider/script-completion-provider';
import { getRegistryForRoot, addToRegistry, REGISTRY_KIND, normalizeMatchNaming, IRegistry, getRegistryForRoots } from './utils/registry-api';
import {
getRegistryForRoot,
addToRegistry,
REGISTRY_KIND,
normalizeMatchNaming,
IRegistry,
getRegistryForRoots,
disableTemplateTokensCollection,
enableTemplateTokensCollection,
} from './utils/registry-api';
import { Usage, findRelatedFiles, waitForTokensToBeCollected, getAllTemplateTokens, ITemplateTokens } from './utils/usages-api';
import { URI } from 'vscode-uri';
import { MatchResultType } from './utils/path-matcher';
Expand All @@ -58,6 +67,10 @@ import { debounce } from 'lodash';
import { Config, Initializer } from './types';
import { isFileBelongsToRoots, mGetProjectAddonsInfo } from './utils/layout-helpers';

export interface IServerConfig {
local: Config;
}

export default class Server {
initializers: Initializer[] = [];
lazyInit = false;
Expand Down Expand Up @@ -108,6 +121,12 @@ export default class Server {
} else if (config.useBuiltinLinting === true) {
this.templateLinter.enable();
}

if (config.collectTemplateTokens === false) {
disableTemplateTokensCollection();
} else if (config.collectTemplateTokens === true) {
enableTemplateTokensCollection();
}
}

documentSymbolProviders: DocumentSymbolProvider[] = [new JSDocumentSymbolProvider(), new HBSDocumentSymbolProvider()];
Expand All @@ -130,7 +149,7 @@ export default class Server {
this.connection.workspace.onDidChangeWorkspaceFolders(this.onDidChangeWorkspaceFolders.bind(this));
}

this.executors['els.setConfig'] = async (_, __, [config]: [{ local: Config }]) => {
this.executors['els.setConfig'] = async (_, __, [config]: [IServerConfig]) => {
this.setConfiguration(config.local);

if (this.lazyInit) {
Expand Down
7 changes: 4 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export type Initializer = () => void;

export interface Config {
addons: string[];
ignoredProjects: string[];
useBuiltinLinting: boolean;
addons?: string[];
ignoredProjects?: string[];
useBuiltinLinting?: boolean;
collectTemplateTokens?: boolean;
}
6 changes: 6 additions & 0 deletions src/utils/path-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ export class ClassicPathMatcher {
metaFromPath(rawAbsoluteAbsPath: string): MatchResult | null {
const rawAbsPath = path.relative(this.root, path.resolve(rawAbsoluteAbsPath));
const normalizedAbsPath = rawAbsPath.split(path.sep).join('/');

// likely it's not a case for classic path matcher
if (normalizedAbsPath.includes('__')) {
return null;
}

const absPath = '/' + normalizedAbsPath;
const isTest = isTestFile(absPath);
const isTemplate = isTemplatePath(absPath);
Expand Down
18 changes: 16 additions & 2 deletions src/utils/registry-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ export function getGlobalRegistry() {
return GLOBAL_REGISTRY;
}

let _templateTokensCollectionEnabled = true;

export function disableTemplateTokensCollection() {
_templateTokensCollectionEnabled = false;
}

export function enableTemplateTokensCollection() {
_templateTokensCollectionEnabled = true;
}

export function canCollectTemplateTokens() {
return _templateTokensCollectionEnabled;
}

const GLOBAL_REGISTRY: {
transform: GLOBAL_REGISTRY_ITEM;
helper: GLOBAL_REGISTRY_ITEM;
Expand Down Expand Up @@ -60,7 +74,7 @@ export function removeFromRegistry(normalizedName: string, kind: REGISTRY_KIND,
files.forEach((file) => {
regItem.delete(file);

if (isTemplatePath(file)) {
if (isTemplatePath(file) && canCollectTemplateTokens()) {
updateTemplateTokens(kind as UsageType, normalizedName, null);
}
});
Expand Down Expand Up @@ -159,7 +173,7 @@ export function addToRegistry(normalizedName: string, kind: REGISTRY_KIND, files

regItem.add(file);

if ((kind === 'component' || kind === 'routePath') && isTemplatePath(file)) {
if (canCollectTemplateTokens() && (kind === 'component' || kind === 'routePath') && isTemplatePath(file)) {
updateTemplateTokens(kind, normalizedName, file);
}
});
Expand Down
6 changes: 3 additions & 3 deletions test/batman-fixture-based-integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { URI } from 'vscode-uri';
import { startServer, initServer, reloadProjects, openFile, normalizeUri, fsProjectToJSON } from './test_helpers/integration-helpers';
import { createMessageConnection, MessageConnection, Logger, StreamMessageReader, StreamMessageWriter } from 'vscode-jsonrpc/node';

import { CompletionRequest, DefinitionRequest } from 'vscode-languageserver-protocol/node';
import { CompletionRequest, Definition, DefinitionRequest } from 'vscode-languageserver-protocol/node';

describe('With `batman project` initialized on server', () => {
let connection: MessageConnection;
Expand Down Expand Up @@ -103,7 +103,7 @@ describe('With `batman project` initialized on server', () => {

openFile(connection, applicationTemplatePath);

let response = await connection.sendRequest(DefinitionRequest.method, params);
let response: Definition[] = await connection.sendRequest(DefinitionRequest.method, params);

response = normalizeUri(response, base);

Expand All @@ -126,7 +126,7 @@ describe('With `batman project` initialized on server', () => {

openFile(connection, applicationTemplatePath);

let response = await connection.sendRequest(DefinitionRequest.method, params);
let response: Definition[] = await connection.sendRequest(DefinitionRequest.method, params);

response = normalizeUri(response, base);

Expand Down
18 changes: 10 additions & 8 deletions test/bultin-addons/core/template-complation-provider-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { filter } from 'fuzzaldrin';
import Server from '../../../src/server';
import { generateNamespacedComponentsHashMap } from '../../../src/builtin-addons/core/template-completion-provider';
import { AddonMeta } from '../../../src/utils/addon-api';

describe('filter util', function () {
it('able to return some results if search term is empty', function () {
Expand All @@ -22,32 +24,32 @@ describe('filter util', function () {

describe('generateNamespacedComponentsHashMap', function () {
it('[Angle brackets] returns the expected namespaced map', function () {
const mockAddonMetaArr = [
const mockAddonMetaArr: AddonMeta[] = [
{ name: '@company/foo', version: 1, root: 'blah/bar/dummy/@company/foo' },
{ name: 'biz', version: 1, root: 'blah/baz/diz/biz' },
];

const server: any = {
getRegistry(root) {
const server = {
getRegistry() {
return { component: { foo: ['blah/baz/diz/biz/components/foo.js'] } };
},
};

expect(generateNamespacedComponentsHashMap(mockAddonMetaArr, server, true)).toEqual({ Foo: ['Biz$Foo'] });
expect(generateNamespacedComponentsHashMap(mockAddonMetaArr, (server as unknown) as Server, true)).toEqual({ Foo: ['Biz$Foo'] });
});

it('[Mustache] returns the expected namespaced map', function () {
const mockAddonMetaArr = [
const mockAddonMetaArr: AddonMeta[] = [
{ name: '@company/test', version: 1, root: 'blah/bar/dummy/@company/test' },
{ name: 'biz', version: 1, root: 'blah/baz/diz/biz' },
];

const server: any = {
getRegistry(root) {
const server = {
getRegistry() {
return { component: { foo: ['blah/bar/dummy/@company/test/components/foo.js'] } };
},
};

expect(generateNamespacedComponentsHashMap(mockAddonMetaArr, server, false)).toEqual({ foo: ['test$foo'] });
expect(generateNamespacedComponentsHashMap(mockAddonMetaArr, (server as unknown) as Server, false)).toEqual({ foo: ['test$foo'] });
});
});
12 changes: 6 additions & 6 deletions test/fixture-based-integration-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { URI } from 'vscode-uri';
import { startServer, initServer, reloadProjects, openFile, normalizeUri } from './test_helpers/integration-helpers';
import { createMessageConnection, MessageConnection, Logger, StreamMessageReader, StreamMessageWriter } from 'vscode-jsonrpc/node';

import { CompletionRequest, DefinitionRequest } from 'vscode-languageserver-protocol/node';
import { CompletionRequest, Definition, DefinitionRequest } from 'vscode-languageserver-protocol/node';

describe('With `full-project` initialized on server', () => {
let connection: MessageConnection;
Expand Down Expand Up @@ -158,7 +158,7 @@ describe('With `full-project` initialized on server', () => {

openFile(connection, definitionTemplatePath);

let response = await connection.sendRequest(DefinitionRequest.method, params);
let response: Definition[] = await connection.sendRequest(DefinitionRequest.method, params);

response = normalizeUri(response, base);
expect(response).toMatchSnapshot();
Expand All @@ -179,7 +179,7 @@ describe('With `full-project` initialized on server', () => {

openFile(connection, definitionTemplatePath);

let response = await connection.sendRequest(DefinitionRequest.method, params);
let response: Definition[] = await connection.sendRequest(DefinitionRequest.method, params);

response = normalizeUri(response, base);
expect(response).toMatchSnapshot();
Expand All @@ -200,7 +200,7 @@ describe('With `full-project` initialized on server', () => {

openFile(connection, modelPath);

let response = await connection.sendRequest(DefinitionRequest.method, params);
let response: Definition[] = await connection.sendRequest(DefinitionRequest.method, params);

response = normalizeUri(response, base);
expect(response).toMatchSnapshot();
Expand All @@ -221,7 +221,7 @@ describe('With `full-project` initialized on server', () => {

openFile(connection, modelPath);

let response = await connection.sendRequest(DefinitionRequest.method, params);
let response: Definition[] = await connection.sendRequest(DefinitionRequest.method, params);

response = normalizeUri(response, base);
expect(response).toMatchSnapshot();
Expand All @@ -242,7 +242,7 @@ describe('With `full-project` initialized on server', () => {

openFile(connection, modelPath);

let response = await connection.sendRequest(DefinitionRequest.method, params);
let response: Definition[] = await connection.sendRequest(DefinitionRequest.method, params);

response = normalizeUri(response, base);
expect(response).toMatchSnapshot();
Expand Down
6 changes: 3 additions & 3 deletions test/glimmer-utils-test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Position } from 'vscode-languageserver';
import { preprocess } from '@glimmer/syntax';
import { ASTv1, preprocess } from '@glimmer/syntax';

import ASTPath, { getLocalScope, maybeComponentNameForPath, sourceForNode, focusedBlockParamName, maybeBlockParamDefinition } from '../src/glimmer-utils';
import { toPosition } from '../src/estree-utils';
Expand Down Expand Up @@ -62,14 +62,14 @@ describe('glimmer-utils', function () {
`;
const astPath = ASTPath.toPosition(preprocess(input), toPosition(Position.create(2, 2)));

expect(astPath.node.tag).toEqual('Component');
expect((astPath.node as ASTv1.ElementNode).tag).toEqual('Component');
expect(sourceForNode(astPath.node, input)).toEqual(input.trim());
});
it('works as expected for MustachePaths', function () {
const input = ['<Component as |items|>', '{{#let items as |item bar|}}', '{{items}}', '{{/let}}', '</Component>'].join('\n');
const astPath = ASTPath.toPosition(preprocess(input), toPosition(Position.create(2, 3)));

expect(astPath.node.original).toEqual('items');
expect((astPath.node as ASTv1.PathExpression).original).toEqual('items');
expect(sourceForNode(astPath.node, input)).toEqual('items');
});
});
Expand Down
3 changes: 2 additions & 1 deletion test/test_helpers/integration-helpers-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('normalizeToFs', () => {
},
};

expect(normalizeToFs(files)).toStrictEqual(JSON.parse(JSON.stringify(files)));
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(normalizeToFs(files as any)).toStrictEqual(JSON.parse(JSON.stringify(files)));
});
it('support new case', () => {
const expectedObj = {
Expand Down
1 change: 1 addition & 0 deletions test/utils/path-matcher-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ describe('ClassicPathMatcher', () => {
expect(m('foo/bar/app/pods/foo/modifier.js')).toEqual(null);
expect(m('foo/bar/app/pods/foo/transform.js')).toEqual(null);
expect(m('foo/bar/app/pods/foo/service.js')).toEqual(null);
expect(m('app/styles/components/ui/meta-attribute__date-range__x-table-form/style.less')).toEqual(null);
});
it('ignores', () => {
expect(m('foo/bar/tmp/app/components/foo/index.ts')).toEqual(null);
Expand Down
35 changes: 29 additions & 6 deletions test/utils/registry-api-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { addToRegistry, getRegistryForRoot, normalizeMatchNaming, removeFromRegistry } from '../../src/utils/registry-api';
import {
addToRegistry,
getRegistryForRoot,
normalizeMatchNaming,
removeFromRegistry,
enableTemplateTokensCollection,
disableTemplateTokensCollection,
canCollectTemplateTokens,
} from '../../src/utils/registry-api';
import { findRelatedFiles, waitForTokensToBeCollected } from '../../src/utils/usages-api';
import { createTempDir } from 'broccoli-test-helper';
import * as path from 'path';
import { MatchResult } from '../../src/utils/path-matcher';
let dir = null;

beforeAll(async () => {
Expand Down Expand Up @@ -59,27 +68,41 @@ describe('addToRegistry - it able to add different kinds to registry', () => {

describe('normalizeMatchNaming - must normalize naming from mater to registry format', () => {
it('normalize special keys', () => {
expect(normalizeMatchNaming({ type: 'route', name: 'foo/bar' })).toEqual({
expect(normalizeMatchNaming({ type: 'route', name: 'foo/bar' } as MatchResult)).toEqual({
type: 'routePath',
name: 'foo.bar',
});
expect(normalizeMatchNaming({ type: 'controller', name: 'foo/bar' })).toEqual({
expect(normalizeMatchNaming({ type: 'controller', name: 'foo/bar' } as MatchResult)).toEqual({
type: 'routePath',
name: 'foo.bar',
});
expect(normalizeMatchNaming({ type: 'template', name: 'foo/bar' })).toEqual({
expect(normalizeMatchNaming({ type: 'template', name: 'foo/bar' } as MatchResult)).toEqual({
type: 'routePath',
name: 'foo.bar',
});
});
it('skip normalization for other keys', () => {
const name = 'foo-bar';

knownRegistryKeys.forEach((keyName) => {
expect(normalizeMatchNaming({ name, type: keyName as any })).toEqual({
knownRegistryKeys.forEach((keyName: unknown) => {
expect(normalizeMatchNaming({ name, type: keyName } as MatchResult)).toEqual({
name,
type: keyName,
});
});
});
});

describe('templateTokens collection toggling', () => {
it('should be enabled by default', () => {
expect(canCollectTemplateTokens()).toBe(true);
});
it('should be able to disable, using disableTemplateTokensCollection', () => {
disableTemplateTokensCollection();
expect(canCollectTemplateTokens()).toBe(false);
});
it('should be able to enable, using enableTemplateTokensCollection', () => {
enableTemplateTokensCollection();
expect(canCollectTemplateTokens()).toBe(true);
});
});
4 changes: 2 additions & 2 deletions test/utils/template-tokens-collector-test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { extractTokensFromTemplate, getTemplateBlocks } from '../../src/utils/template-tokens-collector';

function t(tpl) {
function t(tpl: string) {
return extractTokensFromTemplate(tpl);
}

function tok(tpl) {
function tok(tpl: string) {
return getTemplateBlocks(tpl);
}

Expand Down

0 comments on commit fedd5c0

Please sign in to comment.