Skip to content

Commit

Permalink
Merge #205
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Apr 3, 2022
2 parents 9606afa + 9945270 commit 28c6e35
Show file tree
Hide file tree
Showing 51 changed files with 66,239 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.26 (unreleased)

### Parsing

Added Gleam support.

### Command Line Interface

Added the `--display` option to switch between `side-by-side`,
Expand Down
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ fn main() {
src_dir: "vendor/tree-sitter-elixir-src",
extra_files: vec!["scanner.cc"],
},
TreeSitterParser {
name: "tree-sitter-gleam",
src_dir: "vendor/tree-sitter-gleam-src",
extra_files: vec![],
},
TreeSitterParser {
name: "tree-sitter-go",
src_dir: "vendor/tree-sitter-go-src",
Expand Down
3 changes: 3 additions & 0 deletions sample_files/compare.expected
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ c2d6920b57a543172aef2adfb7aa2df5 -
sample_files/text_before.txt sample_files/text_after.txt
d5bc09cc5298b5216e5989a31238be0b -

sample_files/todomvc_before.gleam sample_files/todomvc_after.gleam
5f57fcf8406d38e26a8fbb1552cc7b63 -

sample_files/typing_before.ml sample_files/typing_after.ml
22eec08aa24c88b330061de913dfce7d -

Expand Down
52 changes: 52 additions & 0 deletions sample_files/todomvc_after.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import todomvc/web/routes
import todomvc/log
import gleam/int
import gleam/string
import gleam/result
import gleam/erlang/os
import gleam/http/elli
import gleam/option
import gleam/pgo

pub fn main() {
log.configure_backend()

let port = load_port()
let application_secret = load_application_secret()
let db = start_database_connection_pool()
let web = routes.stack(application_secret, db)

let log_string =
string.concat(["Listening on localhost:", int.to_string(port), " ✨"])
log.info(log_string)

assert Ok(_) = elli.become(web, on_port: port)
}

pub fn start_database_connection_pool() -> pgo.Connection {
let config =
os.get_env("DATABASE_URL")
|> result.then(pgo.url_config)
|> result.lazy_unwrap(fn() {
pgo.Config(
..pgo.default_config(),
host: "0.0.0.0",
database: "gleam_todomvc_dev",
user: "postgres",
password: option.Some("postgres"),
)
})

pgo.connect(pgo.Config(..config, pool_size: 15))
}

fn load_application_secret() -> String {
os.get_env("APPLICATION_SECRET")
|> result.unwrap("27434b28994f498182d459335258fb6e")
}

fn load_port() -> Int {
os.get_env("PORT")
|> result.then(int.parse)
|> result.unwrap(8080)
}
51 changes: 51 additions & 0 deletions sample_files/todomvc_before.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import todomvc/web/routes
import todomvc/log
import gleam/int
import gleam/string
import gleam/result
import gleam/erlang/os
import gleam/http/elli
import gleam/option
import gleam/pgo

pub fn main() {
log.configure_backend()

let port = load_port()
let application_secret = load_application_secret()
let db = start_database_connection_pool()
let web = routes.stack(application_secret, db)

string.concat(["Listening on localhost:", int.to_string(port), " ✨"])
|> log.info

assert Ok(_) = elli.become(web, on_port: port)
}

pub fn start_database_connection_pool() -> pgo.Connection {
let config =
os.get_env("DATABASE_URL")
|> result.then(pgo.url_config)
|> result.lazy_unwrap(fn() {
pgo.Config(
..pgo.default_config(),
host: "localhost",
database: "gleam_todomvc_dev",
user: "postgres",
password: option.Some("postgres"),
)
})

pgo.connect(pgo.Config(..config, pool_size: 15))
}

fn load_application_secret() -> String {
os.get_env("APPLICATION_SECRET")
|> result.unwrap("27434b28994f498182d459335258fb6e")
}

fn load_port() -> Int {
os.get_env("PORT")
|> result.then(int.parse)
|> result.unwrap(3000)
}
3 changes: 3 additions & 0 deletions src/guess_language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum Language {
Dart,
Elixir,
EmacsLisp,
Gleam,
Go,
Haskell,
JanetSimple,
Expand Down Expand Up @@ -99,6 +100,7 @@ fn from_emacs_mode_header(src: &str) -> Option<Language> {
"c++" => Some(CPlusPlus),
"elixir" => Some(Elixir),
"emacs-lisp" => Some(EmacsLisp),
"gleam" => Some(Gleam),
"go" => Some(Go),
"haskell" => Some(Haskell),
"janet" => Some(JanetSimple),
Expand Down Expand Up @@ -195,6 +197,7 @@ pub fn from_extension(extension: &OsStr) -> Option<Language> {
"dart" => Some(Dart),
"el" => Some(EmacsLisp),
"ex" | "exs" => Some(Elixir),
"gleam" => Some(Gleam),
"go" => Some(Go),
"hs" => Some(Haskell),
"janet" | "jdn" => Some(JanetSimple),
Expand Down
15 changes: 15 additions & 0 deletions src/tree_sitter_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extern "C" {
fn tree_sitter_dart() -> ts::Language;
fn tree_sitter_elisp() -> ts::Language;
fn tree_sitter_elixir() -> ts::Language;
fn tree_sitter_gleam() -> ts::Language;
fn tree_sitter_go() -> ts::Language;
fn tree_sitter_haskell() -> ts::Language;
fn tree_sitter_janet_simple() -> ts::Language;
Expand Down Expand Up @@ -241,6 +242,20 @@ pub fn from_language(language: guess::Language) -> TreeSitterConfig {
.unwrap(),
}
}
Gleam => {
let language = unsafe { tree_sitter_gleam() };
TreeSitterConfig {
name: "Gleam",
language,
atom_nodes: ["string"].into(),
delimiter_tokens: vec![("(", ")"), ("[", "]"), ("{", "}")],
highlight_query: ts::Query::new(
language,
include_str!("../vendor/highlights/gleam.scm"),
)
.unwrap(),
}
}
Go => {
let language = unsafe { tree_sitter_go() };
TreeSitterConfig {
Expand Down
1 change: 1 addition & 0 deletions vendor/highlights/gleam.scm
1 change: 1 addition & 0 deletions vendor/tree-sitter-gleam-src
2 changes: 2 additions & 0 deletions vendor/tree-sitter-gleam/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
src/** linguist-generated
test/** linguist-documentation
37 changes: 37 additions & 0 deletions vendor/tree-sitter-gleam/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: CI

on: [push, pull_request]

jobs:
bless:
name: Test
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install Node
uses: actions/setup-node@v2
with:
node-version: "14.x"

- name: Cache npm dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install npm dependencies
run: npm ci

- name: Ensure generated parser files are up to date
run: npx tree-sitter generate

- name: Run tree-sitter tests
run: npx tree-sitter test

- name: Check formatting
run: npm run check-formatted
43 changes: 43 additions & 0 deletions vendor/tree-sitter-gleam/.github/workflows/generate-parser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# generates the parser with 'tree-sitter generate' if the parser is out of date
name: Generate Parser

on:
push:
branches:
- main

jobs:
generate:
name: Generate Parser
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install Node
uses: actions/setup-node@v2
with:
node-version: "14.x"

- name: Cache npm dependencies
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
- name: Install npm dependencies
run: npm ci

- name: Generate parser files
run: npx tree-sitter generate

- name: Commit generated parser files
run: |
git config --local user.email "$(git log --format='%ae' HEAD^!)"
git config --local user.name "$(git log --format='%an' HEAD^!)"
git add src
git commit -m "Generate parser" || true
git push
58 changes: 58 additions & 0 deletions vendor/tree-sitter-gleam/.github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: release
on:
push:
tags:
- "v*"

jobs:
build-release:
name: build-release
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- macos-latest
- ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Install Node
uses: actions/setup-node@v2
with:
node-version: "14.x"

- name: Install npm dependencies
run: npm ci

- name: Ensure generated parser files are up to date
run: npx tree-sitter generate

- name: Ensure tests pass
run: npx tree-sitter test

- name: Compile library file
run: cc -shared -fPIC -g -O2 -I src src/parser.c -o tree-sitter-gleam.so

- name: Create archive
run: |
VERSION="${GITHUB_REF#refs/tags/}"
case ${{ matrix.os }} in
"ubuntu-latest") FAMILY="linux";;
"macos-latest") FAMILY="macos";;
esac
ARCHIVE="tree-sitter-gleam-$VERSION-$FAMILY.tar.gz"
tar -czf $ARCHIVE tree-sitter-gleam.so
echo "ASSET=$ARCHIVE" >> $GITHUB_ENV
- name: Upload release archive
uses: softprops/action-gh-release@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
draft: true
prerelease: false
fail_on_unmatched_files: true
files: |
${{ env.ASSET }}
5 changes: 5 additions & 0 deletions vendor/tree-sitter-gleam/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules
target
build
*.wasm
log.html
Loading

0 comments on commit 28c6e35

Please sign in to comment.