vscode#SemanticTokensBuilder TypeScript Examples

The following examples show how to use vscode#SemanticTokensBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: semanticProvider.ts    From yash with MIT License 6 votes vote down vote up
export function newSemanticTokenProvider(languageModes: LanguageModes): SemanticTokenProvider {
    const legend = { types: [], modifiers: [] };
    const legendMappings: { [modeId: string]: LegendMapping } = {};

    for (let mode of languageModes.getAllModes()) {
        if (mode.getSemanticTokenLegend && mode.getSemanticTokens) {
            const modeLegend = mode.getSemanticTokenLegend();
            legendMappings[mode.getId()] = { types: createMapping(modeLegend.types, legend.types), modifiers: createMapping(modeLegend.modifiers, legend.modifiers) };
        }
    }

    return {
        legend,
        getSemanticTokens(document: TextDocument, ranges?: Range[]): SemanticTokens {
            const builder = new SemanticTokensBuilder();
            for (let mode of languageModes.getAllModes()) {
                if (mode.getSemanticTokens) {
                    const mapping = legendMappings[mode.getId()];
                    const tokens = mode.getSemanticTokens(document);
                    applyTypesMapping(tokens, mapping.types);
                    applyModifiersMapping(tokens, mapping.modifiers);
                    tokens.forEach(token => {
                        builder.push(token.start.line, token.start.character, token.length, token.typeIdx, token.modifierSet);
                    });
                }
            }
            return builder.build();
        }
    };
}
Example #2
Source File: spSemanticTokenProvider.ts    From sourcepawn-vscode with MIT License 5 votes vote down vote up
export function semanticTokenProvider(
  itemsRepo: ItemsRepository,
  document: TextDocument
): SemanticTokens {
  const tokensBuilder = new SemanticTokensBuilder(SP_LEGENDS);
  let allItems: SPItem[] = itemsRepo.getAllItems(document.uri);

  for (let item of allItems) {
    if (
      item.kind === CompletionItemKind.Constant &&
      item.references !== undefined
    ) {
      for (let ref of item.references) {
        if (ref.uri.fsPath === document.uri.fsPath) {
          tokensBuilder.push(ref.range, "variable", ["readonly"]);
        }
      }
    } else if (item.kind === CompletionItemKind.EnumMember) {
      for (let ref of item.references) {
        if (ref.uri.fsPath === document.uri.fsPath) {
          tokensBuilder.push(ref.range, "enumMember", ["readonly"]);
        }
      }
    } else if (item.kind === CompletionItemKind.Function) {
      for (let ref of item.references) {
        if (ref.uri.fsPath === document.uri.fsPath) {
          if (item.range.contains(ref.range)) {
            tokensBuilder.push(ref.range, "function", ["declaration"]);
          } else {
            tokensBuilder.push(
              ref.range,
              "function",
              item.deprecated ? ["deprecated"] : []
            );
          }
        }
      }
    } else if (item.kind === CompletionItemKind.Method) {
      for (let ref of item.references) {
        if (ref.uri.fsPath === document.uri.fsPath) {
          if (item.range.contains(ref.range)) {
            tokensBuilder.push(ref.range, "method", ["declaration"]);
          } else {
            tokensBuilder.push(
              ref.range,
              "method",
              item.deprecated ? ["deprecated"] : []
            );
          }
        }
      }
    } else if (item.kind === CompletionItemKind.Class) {
      for (let ref of item.references) {
        if (ref.uri.fsPath === document.uri.fsPath) {
          if (item.range.contains(ref.range)) {
            tokensBuilder.push(ref.range, "class", ["declaration"]);
          } else {
            tokensBuilder.push(ref.range, "class");
          }
        }
      }
    }
  }
  return tokensBuilder.build();
}