Git Product home page Git Product logo

typescript-lit-html-plugin's Introduction

TypeScript lit-html Plugin

TypeScript server plugin that adds intellisense for lit-html template strings

Build Status

Features

  • IntelliSense for html tags and attributes.
  • Quick info hovers on tags.
  • Formatting support.
  • Auto closing tags.
  • Folding html.
  • CSS completions in style blocks.
  • Works with literal html strings that contain placeholders.

Usage

This plugin requires TypeScript 2.4 or later. It can provide intellisense in both JavaScript and TypeScript files within any editor that uses TypeScript to power their language features. This includes VS Code, Sublime with the TypeScript plugin, Atom with the TypeScript plugin, Visual Studio, and others.

With VS Code

The simplest way to use this plugin is through the lit-html extension. This extension automatically enables the plugin, and also adds syntax highlighting for lit-html template strings and synchronization of settings between VS Code and the plugin.

To use a specific version of this plugin with VS Code, first install the plugin and a copy of TypeScript in your workspace:

npm install --save-dev typescript-lit-html-plugin typescript

Then add a plugins section to your tsconfig.json or jsconfig.json

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-lit-html-plugin"
      }
    ]
  }
}

Finally, run the Select TypeScript version command in VS Code to switch to use the workspace version of TypeScript for VS Code's JavaScript and TypeScript language support. You can find more information about managing typescript versions in the VS Code documentation.

With Sublime

This plugin works with the Sublime TypeScript plugin.

First install the plugin and a copy of TypeScript in your workspace:

npm install --save-dev typescript-lit-html-plugin typescript

And configure Sublime to use the workspace version of TypeScript by setting the typescript_tsdk setting in Sublime:

{
	"typescript_tsdk": "/Users/matb/my-amazing-project/node_modules/typescript/lib"
}

Finally add a plugins section to your tsconfig.json or jsconfig.json and restart Sublime.

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-lit-html-plugin"
      }
    ]
  }
}

With Atom

This plugin works with the Atom TypeScript plugin.

First install the plugin and a copy of TypeScript in your workspace:

npm install --save-dev typescript-lit-html-plugin typescript

Then add a plugins section to your tsconfig.json or jsconfig.json and restart Atom.

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-lit-html-plugin"
      }
    ]
  }
}

To get syntax highlighting for lit-html strings in Atom, consider installing the language-babel extension.

With Visual Studio

This plugin works Visual Studio 2017 15.8+ using the TypeScript 2.5+ SDK.

First install the plugin in your project:

npm install --save-dev typescript-lit-html-plugin

Then add a plugins section to your tsconfig.json.

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-lit-html-plugin"
      }
    ]
  }
}

Then reload your project to make sure the plugin has been loaded properly. Note that jsconfig.json projects are currently not supported in VS.

Configuration

You can configure the behavior of this plugin in plugins section of in your tsconfig or jsconfig.

If you are using lit-html extension for VS Code, you can configure these settings in the editor settings instead of using a tsconfig or jsconfig.

Tags

This plugin adds html IntelliSense to any template literal tagged with html or raw:

import {html} from 'lit-html'

html`
    <div></div>
`

You can enable IntelliSense for other tag names by configuring "tags":

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-lit-html-plugin",
        "tags": [
          "html",
          "template"
        ]
      }
    ]
  }
}

Formatting

The plugin formats html code by default. You can disable this by setting "format.enabled": false

{
  "compilerOptions": {
    "plugins": [
      {
        "name": "typescript-lit-html-plugin",
        "format": { "enabled": false }
      }
    ]
  }
}

Contributing

To build the typescript-lit-html-plugin, you'll need Git and Node.js.

First, fork the typescript-lit-html-plugin repo and clone your fork:

git clone https://github.com/YOUR_GITHUB_ACCOUNT_NAME/typescript-lit-html-plugin.git
cd typescript-lit-html-plugin

Then install dev dependencies:

npm install

The plugin is written in TypeScript. The source code is in the src/ directory with the compiled JavaScript output to the lib/ directory. Kick off a build using the compile script:

npm run compile

Testing

Run the test using the e2e script:

(cd e2e && npm install)
npm run e2e

The repo also includes a vscode launch.json that you can use to debug the tests and the server. The Mocha Tests launch configuration starts the unit tests. Once a test is running and the TypeScript server for it has been started, use the Attach To TS Server launch configuration to debug the plugin itself.

Submitting PRS

You can submit bug fixes and features through pull requests. To get started, first checkout a new feature branch on your local repo:

git checkout -b my-awesome-new-feature-branch

Make the desired code changes, commit them, and then push the changes up to your forked repository:

git push origin my-awesome-new-feature-branch

Then submit a pull request against the Microsoft typescript-lit-html-plugin repository.

Please also see our Code of Conduct.

typescript-lit-html-plugin's People

Contributors

bjornwiberg avatar dependabot[bot] avatar joaomoreno avatar justinribeiro avatar microsoft-github-policy-service[bot] avatar mjbvz avatar noahlaux avatar roblourens avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typescript-lit-html-plugin's Issues

Debugging via VSCode launch config

Hello,

I saw that there is a launch.json configuration, but I could not figure out how to actually debug this plugin. I started the compile watch task, and tried to follow this Typescript guide on debugging language services (it seemed similar), but could not get it to stop at any breakpoints.

I was planning on taking a look at #19, but it's much harder to reason about without some running code for context ๐Ÿ˜…

I could add a section to the README.md if I get some hints to get it working (if it's possible to debug).

Feature request: allow ts-style-plugin configuration

Firstly, thanks for your work on this plugin, it's great to be able to have syntax highlighting and Intellisense for template strings :) I've noticed, though, that ts-styled-plugin gets rather upset (Do not use empty rulesets: ts-styled-plugin(9999)) if there's a style attribute that is populated by a Lit styleMap Directive, i.e.,

<div style=${styleMap(styles}}></div>

While I appreciate the primary issue is upstream in ts-styled-plugin, there is no way when using this plugin to configure it such that it ignores โ€“ย sayย โ€“ย empty style rules, which I would be able to set were I configuring the plugin directly. I think it comes down to https://github.com/microsoft/typescript-lit-html-plugin/blob/main/src/index.ts#L50, which I think is passing through an empty config to ts-styled-plugin.

I was wondering, would it be possible to either take extra config for ts-styled-plugin, or use any relevant global configs rather than setting it to {}?

Thanks!

auto-completion for CSS inside of style tags

In the following snippet:

html`
<style>
  div {
    display: flex;
    alig <-- cursor here
  }
</style>
`

I get alignItems if I press tab instead of align-items. Can this plugin recognize the <style> tag and auto-complete for CSS properties instead?

Signature help being showing too often

VS Code 1.24 registered < as a sig help trigger for jsx. This cause us to show signature help whenever you type < inside a html string. This is generally good, except that we end up showing the sig help for the template function itself instead of for the html tags. We should either show the html sig help or nothing when inside html template strings

Syntax highlight JS parts

In the code snippet gif in the README, the syntax highlighting of HTML looks very nice. However, the ${title} seems to not be highlighted to note that title is a JS variable. Could the dynamic parts of the template literal be syntax highlighted as JS?

Closing tag autocompletion is missing '>'

From @jolleekin on October 15, 2018 12:34

Steps to reproduce

  1. Type <some-tag></

Actual

  • The plugin suggests /some-tag

Expected

  • The plugin suggest /some-tag>

Suggestions

Even better, the plugin could automatically insert the closing tag when user finishes typing the opening tag. For example, the plugin automatically inserts </some-tag> after user types <some-tag> or <some-tag attribute1="value1" attribute2="value2">.

Copied from original issue: mjbvz/vscode-lit-html#52

lit-html.tags does not control syntax highlighting

Trying to get syntax highlighting in css template strings in VS Code using extension. I've tried adding the following to settings.json but it doesn't seem to have any effect. I feel like I must be doing something incorrect... but then again, maybe not?

settings.json:

"lit-html.tags": [
    "html",
    "css"
]

for example, when using css template strings:

import { css } from 'lit-element/lit-element.js';

export const colors = css`
    :host {
        display: block;
        color: blue;
    }
    :host([hidden]) {
        display: none;
    }
`;

IntelliSense support for CustomElements

It would be wicked cool to have intelliSense support for custom elements defined in the code:

import { LitElement, html,  property, customElement } from 'lit-element';

@customElement('my-element')
class MyElement extends LitElement {
    @property({ type: String }) public foo: string;

    render () {
        return html`<span>${this.foo}</span>`
    }
}

const markup = html`
    <!-- Here intelliSense picks up definition for my-element and suggests to use it with attribute foo >
`

QuickInfo for web components / custom elements (Feature Request)

It'd be great if there was a way to get quickinfo for our custom elements through a docstring or something.

To clarify, when I hover a regular html tag, it'll give me a tooltip with quick info on that element.
image

I'd like to have the same for my custom elements, potentially by adding a class docstring in the View-Model of the custom element?
image

I figure my use of custom-elements might be too specific to the Aurelia framework and this might not be nearly as practical as I might like to think haha. So no worries if it's not a reasonable request.

Syntax error (colon expected) with style attribute (in html template literal)

From @ncodefun on July 15, 2018 21:32

I'm reporting this here since it's this plugin that reports the error, but the context is that I work with lit-html and the vscode-lit-html extension ... So I have this:
ts-styled-error

This only happens with the style attribute, and only when the template string is multi-line. Tried with other styles before or after the expression and it doesn't work either (I thought maybe it looks for at least one colon/semi-colon pair).

Copied from original issue: microsoft/typescript-styled-plugin#64

Doesn't work in Visual Studio 2017 15.7.4 with TypeScript SDK 2.9.2 installed

I have a Visual Studio 2017 project with .ts files that contain html literals.

In my package.json I have:

  "devDependencies": {
    "typescript": "^2.9.2",
    "typescript-lit-html-plugin": "^0.3.1"
  }

In my tsconfig.json I have:

    "plugins": [
      {
        "name": "typescript-lit-html-plugin",
        "tags": [
          "html",
          "svg",
          "raw"
        ]
      }
    ]

When editing .ts files the literals are just strings.

On Format of nested tags: Overlapping ranges not allowed

Try running format on the following file:

const Nav = function() {
  return html`
    <h1>Sections</h1>
    <ul>
      ${repeat(items, i => i.id, (i, index) =>
        html`
          <li>
            ${index}: ${i.name}
          </li>
        `)}
    </ul>
  `;
}

In VSCode, see error about Overlapping ranges not allowed. This is caused by us trying to apply both sets of edits. The outer edits include a formatting for the inner placeholder which conflicts with the edits returned form the inner placeholder html section

highlighting in html files

Currently this plugin only works inside .js files. It would be useful if this could work inside .html files as well. I write lit-html inside single html file snippets for demos a lot.

For example:

<html>
  <head>
    <script type="module">
      import { html, render } from 'lit-html';
      
      const myTemplate = html`
        <div>Foo</div>
      `;// <-- not highlighted

      render(myTemplate, document.body);
    </script>
  </head>
  <body></body>
</html>

Doesn't work in Visual Studio 2017 15.9.6 with TypeScript SDK 3.1.2 installed

Plugin doesn't work at all, much like #11

package.json

{
  "version": "1.0.0",
  "name": "asp.net",
  "private": true,
  "devDependencies": {
    "@types/kendo-ui": "2019.1.0",
    "del": "3.0.0",
    "gulp": "4.0.0",
    "webpack": "4.29.0",
    "webpack-cli": "3.2.1",
    "typescript": "3.3.0-rc",
    "ts-loader": "5.3.3",
    "typescript-lit-html-plugin": "0.9.0"
  },
  "scripts": {
    "build:webpack": "webpack"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "noImplicitAny": true,
    "noEmitOnError": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "es5",
    "lib": [ "es2015", "es5", "dom" ],
    "plugins": [
      {
        "name": "typescript-lit-html-plugin",
        "tags": [
          "html",
          "template"
        ]
      }
    ]
  },
  "compileOnSave": true
}

the following says "(TS) Cannot find the name 'html'"

var  test = html'
    
';

var test2 = html`

`;

Any help is greatly appreciated, let me know if you need any other information.

html is not defined

I use Electron with NodeJS

module.exports = {
  snippet: () => {
    return html`<div>
      </div>`;
  },
};

html is not defined

typescript-lit-html-plugin in neovim

Is it possible to use the typescript-lit-html-plugin in neovim?
I tried the installation in both the project und the tsserver node_modules but it does not work.

Allow use of comment instead of tag

Sometimes using a tag to trigger the benefits of this plugin on a template string isn't feasible, and it would be nice to be able to substitute a comment instead. For example, instead of doing this:

var template = html`
  <h1>Hello World!</h1>
`

...it would be nice to be able to do this:

var template = /*html*/`
  <h1>Hello World</h1>
`

Configuration settings for html/css validation

@sjmiles noted this in mjbvz/vscode-lit-html#42

I tried to solve this by disabling validation, i.e.

"html.validate.styles": false,
"css.validate": false
which had no effect.

Ideally ts-lit-html-plugin honors those configuration options.

With the update to StyledTemplateLanguageService, I'm wondering what your thoughts are on updating the TsHtmlPluginConfiguration interface to be enable validate and lint configuration options similar to styled? Or if there's a better configuration integration approach I'm not quite seeing.

lit-extended & HTMLElementTagNameMap integration

lit-extended sets properties instead of attributes by default, and adds the name$= syntax for opting-into attributes.

If would be great to understand the set of valid properties and their types from HTMLElementTagNameMap. Integration with custom elements would be relatively easy then:

import {html, render} from 'lit-html/lib/lit-extended';

class MyElement extends HTMLElement {
  someProp: number;
}
customElements.define('my-element', HTMLElement);
declare global {
  interface HTMLElementTagNameMap {
    'my-element': MyElement;
  }
}

// this should have a warning under "${'abc'}" for not being a number
html`
  <my-element someProp=${'abc'}></my-element>
`;

// this should have a warning under somProp for not being property of MyElement
html`
  <my-element somProp=${10}></my-element>
`;

// this should have a warning under<my-elemet> for not being a known element
html`
  <my-elemet></my-elemet>
`;

Basically, we want the same features as JSX type-checking. Additionally, it'd be great to have some way to tell tsc the valid attributes for an element.

Support for Markdown

hey there ๐Ÿ‘‹

it would be awesome if the html highlighting would work for js blocks inside markdown as well.
Here is the use case that we are after - Screenshot from VS Code

Screenshot 2020-02-03 at 21 44 06

would be awesome if that is highlighted ๐Ÿ‘

any chance we can get some pointers on where to start looking to get this supported? e.g. we can contribute but we probably need a little help to get started ๐Ÿค—

Autocomplete seems to be broken

Hi,

Having the latest version of the plugin in VSCode, on mac OS. I'm a long time user, recently switched to a new notebook, but after reinstalling my stack, the plugin doesn't give any suggestions when typing basic HTML tags. Before I could write: .foo => <div class="foo"></div>, but now it doesn't seem to work.

Any ideas or settings I might need to enable to get it working?

Plugin version: v1.11.1
VSCode version: 1.69.1
Osx: 12.4

Attribute formatting breaks when not wrapped with "

highlighting-error

These are expected to be formatted the same way, but the formatting is broken when the attribute is not wrapped with ". The HTML specs allow bare attributes (without " wrapping), so the first example is valid, and should be formatted correctly.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.