{"record":{"id":"82e37d1da8af7214","repo":"neoclide/coc.nvim","slug":"legend-must-be-provided-in-constructor","errorCode":null,"errorMessage":"Legend must be provided in constructor","messagePattern":"Legend must be provided in constructor","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/model/semanticTokensBuilder.ts","lineNumber":79,"sourceCode":"  public push(range: Range, tokenType: string, tokenModifiers?: string[]): void\n  public push(arg0: any, arg1: any, arg2: any, arg3?: any, arg4?: any): void {\n    if (typeof arg0 === 'number' && typeof arg1 === 'number' && typeof arg2 === 'number' && typeof arg3 === 'number' && (typeof arg4 === 'number' || typeof arg4 === 'undefined')) {\n      if (typeof arg4 === 'undefined') {\n        arg4 = 0\n      }\n      // 1st overload\n      return this._pushEncoded(arg0, arg1, arg2, arg3, arg4)\n    }\n    if (Range.is(arg0) && typeof arg1 === 'string' && isStrArrayOrUndefined(arg2)) {\n      // 2nd overload\n      return this._push(arg0, arg1, arg2)\n    }\n    throw new Error('Illegal argument')\n  }\n\n  private _push(range: Range, tokenType: string, tokenModifiers?: string[]): void {\n    if (!this._hasLegend) {\n      throw new Error('Legend must be provided in constructor')\n    }\n    if (range.start.line !== range.end.line) {\n      throw new Error('`range` cannot span multiple lines')\n    }\n    if (!this._tokenTypeStrToInt.has(tokenType)) {\n      throw new Error('`tokenType` is not in the provided legend')\n    }\n    const line = range.start.line\n    const char = range.start.character\n    const length = range.end.character - range.start.character\n    const nTokenType = this._tokenTypeStrToInt.get(tokenType)!\n    let nTokenModifiers = 0\n    if (tokenModifiers) {\n      for (const tokenModifier of tokenModifiers) {\n        if (!this._tokenModifierStrToInt.has(tokenModifier)) {\n          throw new Error('`tokenModifier` is not in the provided legend')\n        }\n        const nTokenModifier = this._tokenModifierStrToInt.get(tokenModifier)!","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/neoclide/coc.nvim/blob/50e974d9692461a69147d5cab146a8d3e439abe4/src/model/semanticTokensBuilder.ts#L61-L97","documentation":"_push requires a token legend: SemanticTokensBuilder must be constructed with { tokenTypes: [...], tokenModifiers: [...] } (or a default legend). If the builder was created without a legend, string token types cannot be mapped to numeric indices, so it throws this error.","triggerScenarios":"Creating new SemanticTokensBuilder() with no legend and then calling push(range, 'keyword', ...) — the Range/string overload always requires a legend to translate the string tokenType.","commonSituations":"Following older examples that used the default vscode legend; forgetting the constructor argument when wiring a custom semantic tokens provider; upgrading coc.nvim where legend became required.","solutions":["Construct the builder with a legend: new SemanticTokensBuilder({ tokenTypes: ['keyword','string',...], tokenModifiers: ['readonly',...] })","Or supply the same tokenTypes/tokenModifiers arrays your server declares in its SemanticTokensLegend","Use the encoded overload (numeric tokenType) if you truly have no legend"],"exampleFix":"// before\nconst builder = new SemanticTokensBuilder()\n// after\nconst builder = new SemanticTokensBuilder({ tokenTypes: ['keyword','string','number'], tokenModifiers: ['readonly','deprecated'] })","handlingStrategy":"validation","validationCode":"if (!builder || !legend || !legend.tokenTypes?.length) {\n  throw new Error('SemanticTokensBuilder requires a legend')\n}","typeGuard":"function hasLegend(b: SemanticTokensBuilder): boolean {\n  return (b as any).legend !== undefined || (b as any)._hasLegend === true\n}","tryCatchPattern":"try {\n  builder.push(range, tokenType)\n} catch (e) {\n  if (e.message.includes('Legend must be provided')) {\n    builder = new SemanticTokensBuilder({ tokenTypes: serverLegend.tokenTypes, tokenModifiers: serverLegend.tokenModifiers })\n  }\n}","preventionTips":["Always construct SemanticTokensBuilder with the server's SemanticTokensLegend","Never rely on default legends when writing custom providers","Centralize legend construction in one module"],"tags":["semantic-tokens","api-misuse","constructor"],"backgroundTag":"missing-legend-configuration","analyzedSha":"50e974d9692461a69147d5cab146a8d3e439abe4","analyzedAt":"2026-08-31T11:17:23.966Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}