Giter Club home page Giter Club logo

vim-jsx's Introduction

vim-jsx

IMPORTANT: This package is deprecated! It apparently broke following changes to the pangloss/vim-javascript JavaScript syntax package circa early 2019. As someone who has (perhaps surprisingly) never written a single line of JSX or React code except to test this package, I don't closely track breaking changes like these, hence the (lamentable) inactivity in this repo.

Fortunately, the community seems to have settled on MaxMEllon/vim-jsx-pretty as the syntax package of choice for up-to-date JSX support. If you're not writing exclusively pre-2019 JavaScript with pre-2019 tooling, consider switching over to that package.

Thanks for following along here, and I hope this package was useful to folks in the years since React first debuted.

If things change and this package becomes actively maintained again, I'll be sure to update this note.

Introduction

Syntax highlighting and indenting for JSX. JSX is a JavaScript syntax transformer which translates inline XML document fragments into JavaScript objects. It was developed by Facebook alongside React.

vim-jsx is not a JavaScript syntax package, so in order to use it, you will also need to choose a base JS highlighter. pangloss/vim-javascript is the recommended package---it is vim-jsx's "official" dependency, and the only package against which it is regularly tested. However, vim-jsx makes a best effort to support other JavaScript syntax packages, including:

  • pangloss/vim-javascript
  • jelera/vim-javascript-syntax
  • othree/yajs

Notably, the system vim JavaScript syntax is not supported, due to its over-simplicity. However, the system XML syntax package is an implicit dependency.

Vim support for inline XML in JS is remarkably similar to the same for PHP, which you can find here.

Troubleshooting

If you're experiencing incorrect highlighting or indenting in your JSX code, please file a GitHub issue which includes the following:

  • A brief affirmation that you've read the README and have installed one of the supported dependencies (and the name of the one you're using).

  • A minimal ~/.vimrc which repros the issue you're having, as well as both a paste and a screenshot of the issue (a paste alone is insufficient, since it doesn't illustrate the specific highlighting or indenting problem). To obtain a minimal ~/.vimrc, simply bisect your ~/.vimrc by adding finish at various points in the file. (You can likewise bisect your included plugins by selectively including only half of them, then a quarter, etc.).

Most of the issues filed result from failures to install vim-javascript or conflicts with existing JS syntax or indent files---so failing to indicate that you've ruled those issues out may result in your issue being closed with minimal comment.

(Please feel free to disregard all this for feature requests.)

Usage

By default, JSX syntax highlighting and indenting will be enabled for files with the .js and .jsx extension. If you would like JSX only in .jsx files, add

let g:jsx_ext_required = 1

to your .vimrc or somewhere in your include path. If you wish to restrict JSX to files with the pre-v0.12 @jsx React.DOM pragma, add

let g:jsx_pragma_required = 1

to your .vimrc or somewhere in your include path.

Frequently Asked Questions

  • How come syntax highlighting doesn't work at all?

This is the only question I'll answer with another question---Which do you think is more likely: (a) this package fails completely and utterly in serving its most fundamental purpose, or (b) user error?

  • Why are my end tags colored differently than my start tags?

vim-jsx is basically the glue that holds JavaScript and XML syntax packages together in blissful harmony. This means that any XML syntax defaults carry over to the XML portions of vim, and it's common for many colorschemes to highlight start and end tags differently due to the system XML syntax defaults.

  • Syntax highlighting seems to work, but breaks highlighting and indenting further down in the file. What's wrong?

This often results from trying to enable XML folding in one's ~/.vimrc (i.e., via let g:xml_syntax_folding = 1). vim-jsx does not support syntax folding, and is not tested with either JavaScript or XML folding enabled.

Installation

Pathogen

The recommended installation method is via Pathogen. Then simply execute

git clone https://github.com/mxw/vim-jsx.git ~/.vim/bundle/vim-jsx

(You can install vim-javascript in an analogous manner.)

Vundle

You can also add vim-jsx using Vundle---just add the following lines to your ~/.vimrc:

Plugin 'pangloss/vim-javascript'
Plugin 'mxw/vim-jsx'

To install from within vim, use the commands below.

:so ~/.vimrc
:PluginInstall

Alternatively, use the command below to install the plugins from the command line.

vim +PluginInstall +qall

Manual Installation

If you have no ~/.vim/after directory, you can download the tarball or zip and copy the contents to ~/.vim.

If you have existing ~/.vim/after files, copy the syntax and indent files directly into their respective destinations. If you have existing after syntax or indent files for Javascript, you'll probably want to do something like

mkdir -p ~/.vim/after/syntax/javascript
cp path/to/vim-jsx/after/syntax/jsx.vim ~/.vim/after/syntax/javascript/jsx.vim
mkdir -p ~/.vim/after/indent/javascript
cp path/to/vim-jsx/after/indent/jsx.vim ~/.vim/after/indent/javascript/jsx.vim

vim-jsx's People

Contributors

baskerville avatar bounceme avatar burninglutz avatar dchest avatar frandias avatar golmansax avatar igemnace avatar ingara avatar jambonrose avatar jbaiter avatar jez avatar jiulongw avatar ksmithbaylor avatar lencioni avatar maniart avatar mike-hearn avatar mxw avatar othree avatar pdf avatar rafaelkallis avatar raushan avatar simonratner avatar stoicloofah 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

vim-jsx's Issues

Use @jsx pragma to enable vim-jsx

Currently vim-jsx seem to be turned on for any JavaScript file. Is it possible to detect the presence of

/** @jsx [whatever] */

pragma and only enable jsxRegion when it's present?

Thanks!

JSX pragma detection fails because of non POSIX grep options

On my system, grep doesn't provide any -P or -z option, hence, grep -Pz '(?s)/\*(?:.(?!\*\*/))*jsx.*?\*/' always fails (on a side note: maybe React.DOM should be matched against?).

I would recommend not to use grep, vim can handle this by itself:

autocmd BufNewFile,BufRead * if getline(1) =~ 'REGEX' | SUCCESS | endif

Everything is jsxRegion after a tag

Hey,

Is it me and my setup or does the simplest example not work?

var React = require("react")

React.createClass({
  render: function() {
    return <div>{this.renderMore()}</div>
  },

  renderMore: function() {
    return <strong>Hello</strong>
  }
})

I see renderMore and its function keyword colored incorrectly. Further inspection tells that function is still in the jsxRegion context, which I presume should've ended at the </div> above.

Set filetype to `javascript.jsx`?

In Vim, you can set multiple filetypes by separating them with a .. This would allow you to have special configuration that applies to only JSX files while inheriting all of the configuration for JavaScript files.

Would you be open to a pull request that sets the filetype to javascript.jsx instead of just javascript, and also moves the other files to only work on jsx filetypes?

Setting syntax in vim

Is there a way to set the syntax in a blank file, for example using

:set syntax=jsx

to turn on highlighting? Or is this planned in the future?

auto-indentation broken

auto-indent doesn't work in jsx files for me currently
specifically, chunks of code get un-indented automatically

For example, the "handleUserInput" function on line 36

step1
step2

No highlighting for children

Consider this fragment:

render() {
   return (
      <div>
         {this.props.children}
      </div>
   );
}

The children props section isn't highlighted.
Thanks for writing this bundle, really helpful!

breaks syntax highlighting

So I use this plugin along with pangloss/vim-javascript and I noticed that it breaks parts of the syntax highlighting when writing jsx:

broken syntax

omitting the jsx part seems to fix it
ok syntax

can anyone confirm this?

indent error

for example:

var About = React.createClass({
  render() {/*cursor is here*/}
});

when I typed 'enter',

var About = React.createClass({
 render() {
 }
 });

No syntax highlighting inside blocks

Seeing some very strange behavior now where JSX isn't highlighted inside of any kind of JS block. For example:

screen shot 2015-04-13 at 2 58 01 pm

Works fine for JSX outside of a block. Don't really know how to test this any further; happy to provide more information. Both vim-jsx and vim-javascript are up-to-date.

No highlighting in .es6 files

Hello,

I'm using react with babeljs transpiler and .es6 extensions. In such files JSX is not highlighted. In .jsx files everything is ok. Any ideas? Thanks.

osx
vim 4.7.712
all plugins are up to date.
vimrc

Clobbers my local ftdetect

I'd like to use vim-jsx for the syntax highlighting and indentation features it provides, but it is clobbering my local ftdetect file which I rely on for other things in Vim. I think this is happening because everything in this plugin is nested in an after directory. Would you be open to a pull request that un-nests everything from the after directory?

Support for Airbnb-style indentation

I prefer this style of indenting components, with the closing bracket of the opening tag on its own line, at the same indentation level as the opening bracket:

<Foo
  superLongParam="bar"
  anotherSuperLongParam="baz"
>
  <Spazz />
</Foo>

(See https://github.com/airbnb/javascript/tree/master/react#alignment for more details)

However, by default using this plugin the closing bracket is indented one level more than the start of the opening tag, like so:

<Foo
  superLongParam="bar"
  anotherSuperLongParam="baz"
  >
  <Spazz />
</Foo>

Self-closing tags already work as expected:

<Foo
  hello="world"
  bar="baz"
/>

Highlighting somewhat broken after first jsx region

It seemed like after the first region of JSX in my file, the highlighting would break and believe it's inside of a jsx region.

The culprit for me was " let g:xml_syntax_folding=1.

Throwing this in the knowledge base here because I spent an hour tracking this down ;)

Installation not working

Greetings,

I used pathogen to install the vim-javascript plugin first and then this plugin, but I'm not seeing any of the highlighting. I have Vim 7.2 and have tried new shell sessions. Any help would be much appreciated. Thanks -Dennis

Possible highlighting issue

Hi there. I've installed this plugin and also its dependency via vundle. In some cases it seems to work just fine but I'm curious about two things.

  1. In this case, would it be expected that the closing div is highlighted in a different color than the opening div?

screen shot 2015-02-05 at 12 04 36 pm

  1. If I add an opening quote to the content of my element, highlighting seems to go awry.

screen shot 2015-02-05 at 12 05 41 pm

I can't be 100% sure if this is just an issue of improper installation or if these are features/problems inherent to this plugin.

Interest in Vim JSX compilation support?

Hi all,

One feature I enjoyed and benefitted from when starting out with CoffeeScript, was the vim-coffee-script plugin, which provides the ability to easily 'preview' the compiled Javascript while working in Vim, as a means of debugging and learning.

I've put together a working example of how this could work for JSX, which can be found here: https://github.com/mrjamesriley/jsx-compiler-vim - it works by calling out to the system, to run a JS script which in turns compiles the passed in JSX using the official react-tools, returning the compiled JS, before outputting this into a new buffer within Vim. The middle-man step of a JS script seems necessary here given that the npm jsx does not seem to support standard steams.

I'm relatively new to VimL, but enjoying the process - is there interest in having vim-jsx provide such functionality? I'd happily put the time in to get this functionality added to this plugin - what do you think?

cheers,
James

Parens not balanced correctly.

var x = (
  <div>{{one: 'er'}}</div>
);

Try formatting that with only pangloss's plugin and the parens balance. (Visually select and hit =). Now try it with your plugin as well. The parens do not balance.

Doesn't handle lambda functions

The > in () => expr breaks the syntax highlighting. For example:

<div onClick={() => console.log('foo')} className="will not be highlighted" />

Breaks JS indenting

Hey, sorry to be a pest, but I noticed that with this plugin installed, the indenting on all JS files (.js and .jsx) is broken.

Here's what I mean:

The cursor always returns to the beginning of the line, both in Normal mode and with o or in Insert mode and with Enter. This is with set filetype=javascript and set nopaste.

The JSX syntax highlighting works great, it's just that I really can't work with the indenting broken. Removing this plugin fixes the issue.

Thanks for your work!

Feature request: indenting inside inline XML

Currently, if you use ggVG=, it will correctly indent the Javascript but the XML has no nesting. There is also no indenting when opening and closing tags while typing XML.

The HTML plugin that comes with Vim by default might be a good place to take inspiration, though I'm feeling it wouldn't be trivial to implement.

Conflicts slightly with flow template annotations

If you also use flow annotations (http://flowtype.org/) in your javascript, they can confuse the syntax highlighting because flow template types also use tags, e.g. Array<int>.

The solution for me was simply to add a \W to the front jsx start regex, since real jsx should never immediately follow a word character, and real flow templates should always follow a word character.

If this makes sense to you, it's probably worth adding to master.

Indentation broken

Previous to including this plugin, my indentation worked "as expected", meaning in a reasonably and only semi-broken way. After including this I get no auto-indentation. For example, if I start with this:

var a = React.createClass({
     render: function() {
         return (
             <div>[CURSOR HERE]
         );
     }
});

When I type enter/return I get this:

var a = React.createClass({
     render: function() {
         return (
<div>
    [CURSOR HERE]
         );
     }
});

Without this plugin my vim configuration handles the indentation in a sane way and just doesn't have hilighting. I would love to use this if this gets resolved, and alas I am no vim programmer or I would try to help out.

Quotes in tag-text

Strings should be overridden inside of tags (unless they're further enclosed by curlies.

Example:

<p>I'm an ugly tag!</p>

Curly brace matching is broken

It seems that open curlies inside of JSX are ignored for brace matching, but close curlies are not. This unbalances the curly count outside the JSX, significantly reducing the value of %.

Syntax broken for onClick

For the following code, the closing brace is highlighted in a different way. It also breaks indentation when inside a conditional, as the closing brace is seen as closing the conditional.

<div onClick={this.props.onClick}>

2015-08-07-114618_403x54_scrot

React 0.12 will remove the pragma.

React 0.12 will remove the jsx pragma.

react 0.12-rc

I think possible solutions to detecting a jsx file without the jsx ext should be discussed.

One possible solution is to grep the doc for React. I am not the greatest at vimscript so I don't know how feasible this would be.

Indentation not working.

Using auto indent of entire file all the JSX code gets the same indentation. Does this plugin only provide syntax highlight?

Curly braces dont match

Hi!
Im seeing that curly braces inside a Jsx element dont match correctly in terms of color:

<textarea
        className="message-composer"
        name="message"
        value={this.state.text}
        onChange={this._onChange}
        onKeyDown={this._onKeyDown}
      />

The braces Im referring to are the ones in the attribute values, i.e., value, onChange and onKeyDown.

Thanks!
Fran

No highlighting

I have Installed 'pangloss/vim-javascript' and in the top of my jsx file I have /** @jsx React.DOM */
I am using Vundle. Did try commenting out all Bundles except vim-jsx and vim-javascript.
Still can't get it to work.

Could it perhaps be something else in my .vimrc that's causing the issue?

Highlighting issue.

I am having an issue with the xml/html not being highlighted. I am using vam(vim addon manager) and when I check :scriptnames I see the jsx files. I can run set ft=jsx and the xml/html is highlighted but the javascript is not. But just loading up a jsx file has the filetype as javascript and the xml/html regions are not highlighted.

Why is everything in the after directory?

I believe the after directory is meant for customizing existing syntaxes and plugins. JSX.vim is a plugin itself, so shouldn't it live outside the after directory?

Enabling different shiftwidth in JSX indentation

Hi, I appreciate this plugin a lot in my everyday work.
I am writing quite a lot of JSX markup in my js files, with intentation 4, which makes the file unreadable on small viewports due to line breaks.

I understand that on JSX syntax the plugin enables the same rules as XML, I was wondering how difficult it should be to enable set sw=2 only for the JSX syntax.
Thanks

Breaks HTML syntax highlight

The following HTML document is not highlighted properly when vim-jsx is installed:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Foo</title>
        <script src="foo.js"></script>
    </head>
    <body>
    </body>
</html>

The problem starts at script which is tagged as xmlTagName instead of htmlSpecialTagName.

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.