Skip to content

Commit

Permalink
Let targets remove categories from blocks (#4533)
Browse files Browse the repository at this point in the history
* Let targets remove categories from blocks

* Fix math search and gracefully fail when there are no results
  • Loading branch information
riknoll committed Jul 6, 2018
1 parent 97e7f11 commit 0fe3f48
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 14 deletions.
1 change: 1 addition & 0 deletions localtypings/pxtarget.d.ts
Expand Up @@ -89,6 +89,7 @@ declare namespace pxt {
assetExtensions?: string[];
palette?: string[];
screenSize?: Size;
bannedCategories?: string[]; // a list of categories to exclude blocks from
}

interface AppAnalytics {
Expand Down
4 changes: 2 additions & 2 deletions pxtcompiler/emitter/driver.ts
Expand Up @@ -212,13 +212,13 @@ namespace ts.pxtc {
return res
}

export function decompile(opts: CompileOptions, fileName: string, includeGreyBlockMessages = false) {
export function decompile(opts: CompileOptions, fileName: string, includeGreyBlockMessages = false, bannedCategories?: string[]) {
const resp = compile(opts);
if (!resp.success) return resp;

let file = resp.ast.getSourceFile(fileName);
const apis = getApiInfo(opts, resp.ast);
const blocksInfo = pxtc.getBlocksInfo(apis);
const blocksInfo = pxtc.getBlocksInfo(apis, bannedCategories);
const bresp = pxtc.decompiler.decompileToBlocks(blocksInfo, file, { snippetMode: false, alwaysEmitOnStart: opts.alwaysDecompileOnStart, includeGreyBlockMessages }, pxtc.decompiler.buildRenameMap(resp.ast, file))
return bresp;
}
Expand Down
12 changes: 7 additions & 5 deletions pxtcompiler/emitter/service.ts
Expand Up @@ -573,15 +573,15 @@ namespace ts.pxtc.service {
isJsDocTagName: boolean;
}

const blocksInfoOp = (apisInfoLocOverride?: pxtc.ApisInfo) => {
const blocksInfoOp = (apisInfoLocOverride?: pxtc.ApisInfo, bannedCategories?: string[]) => {
if (apisInfoLocOverride) {
if (!lastLocBlocksInfo) {
lastLocBlocksInfo = getBlocksInfo(apisInfoLocOverride);
lastLocBlocksInfo = getBlocksInfo(apisInfoLocOverride, bannedCategories);
}
return lastLocBlocksInfo;
} else {
if (!lastBlocksInfo) {
lastBlocksInfo = getBlocksInfo(lastApiInfo);
lastBlocksInfo = getBlocksInfo(lastApiInfo, bannedCategories);
}
return lastBlocksInfo;
}
Expand Down Expand Up @@ -625,7 +625,8 @@ namespace ts.pxtc.service {
return compile(v.options)
},
decompile: v => {
return decompile(v.options, v.fileName);
const bannedCategories = v.blocks ? v.blocks.bannedCategories : undefined;
return decompile(v.options, v.fileName, false, bannedCategories);
},
assemble: v => {
return {
Expand Down Expand Up @@ -672,7 +673,8 @@ namespace ts.pxtc.service {
apiSearch: v => {
const SEARCH_RESULT_COUNT = 7;
const search = v.search;
const blockInfo = blocksInfoOp(search.localizedApis); // cache
const bannedCategories = v.blocks ? v.blocks.bannedCategories : undefined;
const blockInfo = blocksInfoOp(search.localizedApis, bannedCategories); // cache

if (search.localizedStrings) {
pxt.Util.setLocalizedStrings(search.localizedStrings);
Expand Down
26 changes: 24 additions & 2 deletions pxtlib/service.ts
Expand Up @@ -411,8 +411,8 @@ namespace ts.pxtc {
return n ? Util.capitalize(n.split('.')[0]) : undefined;
}

export function getBlocksInfo(info: ApisInfo): BlocksInfo {
const blocks: SymbolInfo[] = []
export function getBlocksInfo(info: ApisInfo, categoryFilters?: string[]): BlocksInfo {
let blocks: SymbolInfo[] = []
const combinedSet: pxt.Map<SymbolInfo> = {}
const combinedGet: pxt.Map<SymbolInfo> = {}
const combinedChange: pxt.Map<SymbolInfo> = {}
Expand Down Expand Up @@ -584,12 +584,29 @@ namespace ts.pxtc {
}
}

if (pxt.appTarget && pxt.appTarget.runtime && Array.isArray(pxt.appTarget.runtime.bannedCategories)) {
filterCategories(pxt.appTarget.runtime.bannedCategories)
}

if (categoryFilters) {
filterCategories(categoryFilters);
}

return {
apis: info,
blocks,
blocksById: pxt.Util.toDictionary(blocks, b => b.attributes.blockId),
enumsByName
}

function filterCategories(banned: string[]) {
if (banned.length) {
blocks = blocks.filter(b => {
let ns = (b.attributes.blockNamespace || b.namespace).split('.')[0];
return banned.indexOf(ns) === -1;
});
}
}
}

export function localizeApisAsync(apis: pxtc.ApisInfo, mainPkg: pxt.MainPackage): Promise<pxtc.ApisInfo> {
Expand Down Expand Up @@ -1353,6 +1370,7 @@ namespace ts.pxtc.service {
options?: CompileOptions;
search?: SearchOptions;
format?: FormatOptions;
blocks?: BlocksOptions;
}

export interface SearchOptions {
Expand All @@ -1378,6 +1396,10 @@ namespace ts.pxtc.service {
localizedCategory?: string;
builtinBlock?: boolean;
}

export interface BlocksOptions {
bannedCategories?: string[];
}
}

namespace ts.pxtc.ir {
Expand Down
11 changes: 9 additions & 2 deletions webapp/src/compiler.ts
Expand Up @@ -161,7 +161,7 @@ export function decompileSnippetAsync(code: string, blockInfo?: ts.pxtc.BlocksIn
}

function decompileCoreAsync(opts: pxtc.CompileOptions, fileName: string): Promise<pxtc.CompileResult> {
return workerOpAsync("decompile", { options: opts, fileName: fileName })
return workerOpAsync("decompile", { options: opts, fileName: fileName, blocks: blocksOptions() })
}

export function workerOpAsync(op: string, arg: pxtc.service.OpArg) {
Expand Down Expand Up @@ -196,7 +196,7 @@ export function apiSearchAsync(searchFor: pxtc.service.SearchOptions) {
.then(() => {
searchFor.localizedApis = cachedApis;
searchFor.localizedStrings = pxt.Util.getLocalizedStrings();
return workerOpAsync("apiSearch", { search: searchFor })
return workerOpAsync("apiSearch", { search: searchFor, blocks: blocksOptions() });
});
}

Expand Down Expand Up @@ -238,3 +238,10 @@ export function newProject() {
cachedBlocks = null;
workerOpAsync("reset", {}).done();
}

function blocksOptions(): pxtc.service.BlocksOptions {
if (pxt.appTarget && pxt.appTarget.runtime && pxt.appTarget.runtime.bannedCategories && pxt.appTarget.runtime.bannedCategories.length) {
return { bannedCategories: pxt.appTarget.runtime.bannedCategories };
}
return undefined;
}
15 changes: 13 additions & 2 deletions webapp/src/monaco.tsx
Expand Up @@ -961,7 +961,14 @@ export class Editor extends toolboxeditor.ToolboxEditor {
let monacoFlyout = this.createMonacoFlyout();

if (ns == 'search') {
this.showSearchFlyout();
try {
this.showSearchFlyout();
}
catch (e) {
pxt.reportException(e);
pxsim.U.clear(monacoFlyout);
this.addNoSearchResultsLabel();
}
return;
}

Expand Down Expand Up @@ -1047,10 +1054,14 @@ export class Editor extends toolboxeditor.ToolboxEditor {
this.attachMonacoBlockAccessibility(monacoBlocks);

if (monacoBlocks.length == 0) {
this.getMonacoLabel(lf("No search results..."), 'monacoFlyoutLabel');
this.addNoSearchResultsLabel();
}
}

private addNoSearchResultsLabel() {
this.getMonacoLabel(lf("No search results..."), 'monacoFlyoutLabel');
}

private getMonacoFlyout() {
return document.getElementById('monacoFlyoutWidget');
}
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/monacoSnippets.ts
Expand Up @@ -129,7 +129,9 @@ function cachedBuiltinCategories(): pxt.Map<BuiltinCategoryDefinition> {
};
_cachedBuiltinCategories[CategoryNameID.Maths] = {
name: lf("{id:category}Math"),
nameid: 'math',
// Unlike the other categories, this namespace is actually capitalized in
// the source so the nameid must be capitalized also
nameid: 'Math',
blocks: [
{
name: "Math.plus",
Expand Down

0 comments on commit 0fe3f48

Please sign in to comment.