npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@val-town/codemirror-ts

v1.0.0-alpha.0

Published

codemirror extensions for typescript

Downloads

113

Readme

codemirror-ts

TypeScript extensions for CodeMirror. This aims to support as much of the basic interactions with TypeScript code as possible in CodeMirror.

Currently provides

  • Hover hints for types
  • Autocomplete
  • Diagnostics (lints, in CodeMirror's terminology)

Peer dependencies

This module does not depend on anything: your project should have direct dependencies to:

  • @codemirror/view
  • @codemirror/lint
  • @codemirror/autocomplete

Using these extensions

This is designed to scale up to more complex scenarios, so there is some assembly required. We could encapsulate more, but that would mean removing important points of control.

  1. Create a TypeScript environment.

We don't create a TypeScript environment for you. This you bring, and it probably is used for other parts of your application. The simplest setup would be something like this, using @typescript/vfs:

import {
  createDefaultMapFromCDN,
  createSystem,
  createVirtualTypeScriptEnvironment,
} from "@typescript/vfs";

const fsMap = await createDefaultMapFromCDN(
  { target: ts.ScriptTarget.ES2022 },
  "3.7.3",
  true,
  ts,
);
const system = createSystem(fsMap);
const compilerOpts = {};
const env = createVirtualTypeScriptEnvironment(system, [], ts, compilerOpts);
  1. Install the sync extension:

This extension powers the rest: when you make changes in your editor, this mirrors them to the TypeScript environment using createFile and updateFile in the TypeScript compiler.

Note, also, that we're supplying a path. These extensions use file paths in order to differentiate between different editor instances and to allow editors to import & export to & from one another. So each extension has a required path parameter, as well as the env parameter which should be your TypeScript environment.

import { tsSync } from "@valtown/codemirror-ts";

let env = "index.ts";

let editor = new EditorView({
  extensions: [
    basicSetup,
    javascript({
      typescript: true,
      jsx: true,
    }),
    tsSync({ env, path }),
  ],
  parent: document.querySelector("#editor"),
});

Linting

The tsLinter extension can be initialized like this and added to the extensions array in the setup of your CodeMirror instance.

tsLinter({ env, path });

This uses the @codemirror/lint package and grabs diagnostics from the TypeScript environment.

If you want to modify how lints are handled, you can use the getLints({ env, path }) method and wire it up with CodeMirror's linter method yourself.

Autocompletion

To make it possible to combine different autocompletion sources, we expose a [CompletionSource]((https://codemirror.net/docs/ref/#autocomplete.autocompletion) which you can use with the CodeMirror autocomplete method:

autocompletion({
  override: [tsAutocomplete({ env, path })],
});

We expose a lower-level interface to autocompletions with the getAutocompletion({ env, path, context }) method that takes a CompletionContext parameter.

Hover

The hover definition can be used like the following:

tsHover({
  env,
  path,
});

Which automatically uses a default renderer. However, you can customize this to your heart's content, and use your web framework to render custom UI if you want to, using the renderTooltip option.

tsHover({
  env,
  path,
  renderTooltip: (info: HoverInfo) => {
    const div = document.createElement("div");
    if (info.quickInfo?.displayParts) {
      for (let part of info.quickInfo.displayParts) {
        const span = div.appendChild(document.createElement("span"));
        span.className = `quick-info-${part.kind}`;
        span.innerText = part.text;
      }
    }
    return { dom: div };
  },
});

Roadmap

  • [ ] Support for a TypeScript environment in a WebWorker