UNPKG

pxt-core

Version:

Microsoft MakeCode provides Blocks / JavaScript / Python tools and editors

1 lines 2.79 kB
const worker=(t,e,n)=>class extends t{constructor(t,n){super(t,n),this.weightCache={},this.service=e.createLanguageService(this)}async getCompletionsAtPosition(t,n){const o=await super.getCompletionsAtPosition(t,n);if(!o)return o;o.entries=o.entries.filter((t=>!t.name.startsWith("_")));const r=this.service.getProgram(),i=r.getSourceFile(t),s=i.getFullText();let c=n-1;for(;c>0&&e.isIdentifierPart(s.charCodeAt(n),e.ScriptTarget.ES5);)c--;if("."!==s.charAt(c)&&(c=-1),c>0){const t=findInnerMostNodeAtPosition(i,c-1,e),n=r.getTypeChecker(),s=n.getTypeAtLocation(t),a=(null==s?void 0:s.symbol)||n.getSymbolAtLocation(t);if(a){const t=n.getFullyQualifiedName(a);if(!this.weightCache[t]){this.weightCache[t]={};const n=[];a.members&&n.push(a.members),a.exports&&n.push(a.exports);const o=parseCommentString(getComments(a,e));let r=[];if(o.groups)try{const t=JSON.parse(o.groups);Array.isArray(r)&&!r.some((t=>"string"!=typeof t))&&(r=t)}catch(t){}const i={},s={};for(const o of n)o.forEach((n=>{const o=parseCommentString(getComments(n,e));let r=0;if("TD_ID"===o.shim||o.hidden||o.deprecated)return void(this.weightCache[t][n.name]="hidden");o.weight&&(r=parseInt(o.weight));const c=o.advanced||!o.block?i:s,a=o.block?o.group||"*":"ts-only";c[a]||(c[a]=[]),c[a].push([n.name,r])}));const c=["*"].concat(r);for(const t of Object.keys(i).concat(Object.keys(s)))-1===c.indexOf(t)&&c.push(t);const h=[];for(const t of c)s[t]&&h.push(...s[t].sort(((t,e)=>e[1]-t[1])).map((t=>t[0])));for(const t of c)i[t]&&h.push(...i[t].sort(((t,e)=>e[1]-t[1])).map((t=>t[0])));for(let e=0;e<h.length;e++)this.weightCache[t][h[e]]=weightToSortText(h.length-e)}o.entries=o.entries.filter((e=>"hidden"!==this.weightCache[t][e.name]));for(const e of o.entries){const n=this.weightCache[t][e.name]||weightToSortText(0);e.sortText=n+e.name}}}return o}};function findInnerMostNodeAtPosition(t,e,n){for(let o of t.getChildren()){if(o.kind>=n.SyntaxKind.FirstPunctuation&&o.kind<=n.SyntaxKind.LastPunctuation)continue;let t=o.getStart(),r=o.getEnd();if(t<=e&&e<r)return findInnerMostNodeAtPosition(o,e,n)}return t&&t.kind===n.SyntaxKind.SourceFile?null:t}function getComments(t,e){return t.getDeclarations()?t.getDeclarations().map((t=>{const n=t.getSourceFile();if(!n)return"";const o=e.getLeadingCommentRangesOfNode(t,n);if(!o)return"";return o.map((t=>n.text.slice(t.pos,t.end))).join("\n")})).join("\n"):""}function parseCommentString(t){const e={};if(!t)return e;let n=!0;for(;n;)n=!1,t=t.replace(/\/\/%[ \t]*([\w\.-]+)(=(("[^"\n]*")|'([^'\n]*)'|([^\s]*)))?/,((t,o,r,i,s,c,a)=>{const h=s?JSON.parse(s):r?s||c||a:"true";return e[o]=h,n=!0,"//% "}));return e}function weightToSortText(t){let e;e="string"==typeof t?parseInt(t):t,e=9999-e;let n=e+"";for(;n.length<4;)n="0"+n;return n}self.customTSWorkerFactory=worker;