Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve diffs inside Elixir sigils #785

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sample_files/compare.expected
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ sample_files/elisp_1.el sample_files/elisp_2.el
sample_files/elisp_contiguous_1.el sample_files/elisp_contiguous_2.el
4a5a33873a4f84ee055d95e1448fba35 -

sample_files/elixir_1.ex sample_files/elixir_2.ex
85494310196ac5065b3b4ce1d4b350fd -

sample_files/elm_1.elm sample_files/elm_2.elm
ccc1f4bb568cd72781dbcd623b612c43 -

Expand Down
13 changes: 13 additions & 0 deletions sample_files/elixir_1.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ExampleComponent do
defp greet do
"Hello world"
end

def greet_component(assigns) do
~H"""
<p class="title">
<%= greet() %>
</p>
"""
end
end
13 changes: 13 additions & 0 deletions sample_files/elixir_2.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ExampleComponent do
defp greet_str do
"Hello world!"
end

def greet_component(assigns) do
~H"""
<p class="title new-class">
<b><%= greet_str() %></b>
</p>
"""
end
end
2 changes: 1 addition & 1 deletion src/parse/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ pub(crate) fn from_language(language: guess::Language) -> TreeSitterConfig {
let language = unsafe { tree_sitter_elixir() };
TreeSitterConfig {
language,
atom_nodes: vec!["string", "heredoc"].into_iter().collect(),
atom_nodes: vec!["string", "sigil", "heredoc"].into_iter().collect(),
delimiter_tokens: vec![("(", ")"), ("{", "}"), ("do", "end")]
.into_iter()
.collect(),
Expand Down
39 changes: 39 additions & 0 deletions vendored_parsers/tree-sitter-elixir/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{json,toml,yml,gyp}]
indent_style = space
indent_size = 2

[*.js]
indent_style = space
indent_size = 2

[*.rs]
indent_style = space
indent_size = 4

[*.{c,cc,h}]
indent_style = space
indent_size = 4

[*.{py,pyi}]
indent_style = space
indent_size = 4

[*.swift]
indent_style = space
indent_size = 4

[*.go]
indent_style = tab
indent_size = 8

[Makefile]
indent_style = tab
indent_size = 8
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Workaround for https://github.com/nodejs/node-gyp/issues/2219#issuecomment-1359162118
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install Node
uses: actions/setup-node@v2
with:
Expand All @@ -24,7 +28,7 @@ jobs:
- name: Generate parser
run: |
npx tree-sitter generate
npx tree-sitter build-wasm
npx tree-sitter build --wasm -o tree-sitter-elixir.wasm
- name: Update parser files
uses: stefanzweifel/git-auto-commit-action@v4
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# Workaround for https://github.com/nodejs/node-gyp/issues/2219#issuecomment-1359162118
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Install Node
uses: actions/setup-node@v2
with:
Expand Down
35 changes: 35 additions & 0 deletions vendored_parsers/tree-sitter-elixir/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,41 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased](https://github.com/elixir-lang/tree-sitter-elixir/tree/main)

### Added

* Support for ~LVN sigils (LiveView Native templates) in the built-in injections ([#75](https://github.com/elixir-lang/tree-sitter-elixir/pull/75))

## [v0.3.1](https://github.com/elixir-lang/tree-sitter-elixir/tree/v0.3.1) (2024-09-28)

### Changed

* Changed highlight queries to distinguish field access from calls ([#73](https://github.com/elixir-lang/tree-sitter-elixir/pull/73))

## [v0.3.0](https://github.com/elixir-lang/tree-sitter-elixir/tree/v0.3.0) (2024-09-05)

### Changed

* The Rust crate to depend on tree-sitter-language rather than tree-sitter ([#70](https://github.com/elixir-lang/tree-sitter-elixir/pull/70))

## [v0.2.0](https://github.com/elixir-lang/tree-sitter-elixir/tree/v0.2.0) (2024-04-08)

### Changed

* Required tree-sitter version to 0.21+ ([#66](https://github.com/elixir-lang/tree-sitter-elixir/pull/66))

## [v0.1.1](https://github.com/elixir-lang/tree-sitter-elixir/tree/v0.1.1) (2023-12-04)

### Changed

* Rewritten the custom scanner code in C ([#56](https://github.com/elixir-lang/tree-sitter-elixir/pull/56))

### Fixed

* Parsing empty interpolation ([#55](https://github.com/elixir-lang/tree-sitter-elixir/pull/55))
* Fixed the repository URL in the Rust crate ([#57](https://github.com/elixir-lang/tree-sitter-elixir/pull/57))

## [v0.1.0](https://github.com/elixir-lang/tree-sitter-elixir/tree/v0.1.0) (2023-03-14)

Initial release.
7 changes: 5 additions & 2 deletions vendored_parsers/tree-sitter-elixir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tree-sitter-elixir"
description = "Elixir grammar for the tree-sitter parsing library"
version = "0.1.0"
version = "0.3.1"
keywords = ["incremental", "parsing", "elixir"]
categories = ["parsing", "text-editors"]
repository = "https://github.com/elixir-lang/tree-sitter-elixir"
Expand All @@ -20,7 +20,10 @@ include = [
path = "bindings/rust/lib.rs"

[dependencies]
tree-sitter = ">= 0.19, < 0.21"
tree-sitter-language = "0.1.0"

[dev-dependencies]
tree-sitter = "0.23.0"

[build-dependencies]
cc = "1.0"
4 changes: 2 additions & 2 deletions vendored_parsers/tree-sitter-elixir/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION := 0.19.1
VERSION := 0.3.1

# Repository
SRC_DIR := src
Expand Down Expand Up @@ -29,7 +29,7 @@ PCLIBDIR ?= $(LIBDIR)/pkgconfig
CPPSRC := $(wildcard $(SRC_DIR)/*.cc)

ifeq (, $(CPPSRC))
ADDITIONALLIBS :=
ADDITIONALLIBS :=
else
ADDITIONALLIBS := -lc++
endif
Expand Down
12 changes: 7 additions & 5 deletions vendored_parsers/tree-sitter-elixir/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@
"targets": [
{
"target_name": "tree_sitter_elixir_binding",
"dependencies": [
"<!(node -p \"require('node-addon-api').targets\"):node_addon_api_except",
],
"include_dirs": [
"<!(node -e \"require('nan')\")",
"src"
"src",
],
"sources": [
"bindings/node/binding.cc",
"src/parser.c",
"src/scanner.c"
"src/scanner.c",
],
"cflags_c": [
"-std=c99",
]
"-std=c11",
],
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
prefix=@PREFIX@
libdir=@LIBDIR@
includedir=@INCLUDEDIR@

Name: tree-sitter-elixir
Description: Elixir grammar for tree-sitter
URL: @URL@
Version: @VERSION@
Requires: @REQUIRES@
Libs: -L${libdir} @ADDITIONAL_LIBS@ -ltree-sitter-elixir
Cflags: -I${includedir}
13 changes: 13 additions & 0 deletions vendored_parsers/tree-sitter-elixir/bindings/go/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tree_sitter_elixir

// #cgo CFLAGS: -std=c11 -fPIC
// #include "../../src/parser.c"
// #include "../../src/scanner.c"
import "C"

import "unsafe"

// Get the tree-sitter Language for this grammar.
func Language() unsafe.Pointer {
return unsafe.Pointer(C.tree_sitter_elixir())
}
15 changes: 15 additions & 0 deletions vendored_parsers/tree-sitter-elixir/bindings/go/binding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tree_sitter_elixir_test

import (
"testing"

tree_sitter "github.com/tree-sitter/go-tree-sitter"
tree_sitter_elixir "github.com/tree-sitter/tree-sitter-elixir/bindings/go"
)

func TestCanLoadGrammar(t *testing.T) {
language := tree_sitter.NewLanguage(tree_sitter_elixir.Language())
if language == nil {
t.Errorf("Error loading Elixir grammar")
}
}
36 changes: 14 additions & 22 deletions vendored_parsers/tree-sitter-elixir/bindings/node/binding.cc
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
#include "tree_sitter/parser.h"
#include <node.h>
#include "nan.h"
#include <napi.h>

using namespace v8;
typedef struct TSLanguage TSLanguage;

extern "C" TSLanguage * tree_sitter_elixir();
extern "C" TSLanguage *tree_sitter_elixir();

namespace {
// "tree-sitter", "language" hashed with BLAKE2
const napi_type_tag LANGUAGE_TYPE_TAG = {
0x8AF2E5212AD58ABF, 0xD5006CAD83ABBA16
};

NAN_METHOD(New) {}

void Init(Local<Object> exports, Local<Object> module) {
Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New);
tpl->SetClassName(Nan::New("Language").ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);

Local<Function> constructor = Nan::GetFunction(tpl).ToLocalChecked();
Local<Object> instance = constructor->NewInstance(Nan::GetCurrentContext()).ToLocalChecked();
Nan::SetInternalFieldPointer(instance, 0, tree_sitter_elixir());

Nan::Set(instance, Nan::New("name").ToLocalChecked(), Nan::New("elixir").ToLocalChecked());
Nan::Set(module, Nan::New("exports").ToLocalChecked(), instance);
Napi::Object Init(Napi::Env env, Napi::Object exports) {
exports["name"] = Napi::String::New(env, "elixir");
auto language = Napi::External<TSLanguage>::New(env, tree_sitter_elixir());
language.TypeTag(&LANGUAGE_TYPE_TAG);
exports["language"] = language;
return exports;
}

NODE_MODULE(tree_sitter_elixir_binding, Init)

} // namespace
NODE_API_MODULE(tree_sitter_elixir_binding, Init)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/// <reference types="node" />

const assert = require("node:assert");
const { test } = require("node:test");

test("can load grammar", () => {
const parser = new (require("tree-sitter"))();
assert.doesNotThrow(() => parser.setLanguage(require(".")));
});
28 changes: 28 additions & 0 deletions vendored_parsers/tree-sitter-elixir/bindings/node/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
type BaseNode = {
type: string;
named: boolean;
};

type ChildNode = {
multiple: boolean;
required: boolean;
types: BaseNode[];
};

type NodeInfo =
| (BaseNode & {
subtypes: BaseNode[];
})
| (BaseNode & {
fields: { [name: string]: ChildNode };
children: ChildNode[];
});

type Language = {
name: string;
language: unknown;
nodeTypeInfo: NodeInfo[];
};

declare const language: Language;
export = language;
18 changes: 3 additions & 15 deletions vendored_parsers/tree-sitter-elixir/bindings/node/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
try {
module.exports = require("../../build/Release/tree_sitter_elixir_binding");
} catch (error1) {
if (error1.code !== 'MODULE_NOT_FOUND') {
throw error1;
}
try {
module.exports = require("../../build/Debug/tree_sitter_elixir_binding");
} catch (error2) {
if (error2.code !== 'MODULE_NOT_FOUND') {
throw error2;
}
throw error1
}
}
const root = require("path").join(__dirname, "..", "..");

module.exports = require("node-gyp-build")(root);

try {
module.exports.nodeTypeInfo = require("../../src/node-types.json");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from unittest import TestCase

import tree_sitter, tree_sitter_elixir


class TestLanguage(TestCase):
def test_can_load_grammar(self):
try:
tree_sitter.Language(tree_sitter_elixir.language())
except Exception:
self.fail("Error loading Elixir grammar")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"Elixir grammar for tree-sitter"

from ._binding import language

__all__ = ["language"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
def language() -> int: ...
Loading
Loading