Skip to content

Commit

Permalink
make this repository usable as Zig dependency for other projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Techatrix committed Jun 23, 2024
1 parent dd40980 commit 94e3e8b
Show file tree
Hide file tree
Showing 6 changed files with 14,928 additions and 25 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Deploy autodoc to Github Pages

on:
push:
branches:
- master

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: "pages"
cancel-in-progress: false

jobs:
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Pages
uses: actions/configure-pages@v5

- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: master

- run: zig build docs --summary all

- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: "zig-out/doc/lsp-codegen"

- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.zig-cache
zig-out
metaModel.json

zig-cache
28 changes: 21 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
# zig-lsp-codegen
[![CI](https://github.com/zigtools/zig-lsp-codegen/actions/workflows/main.yml/badge.svg)](https://github.com/zigtools/zig-lsp-codegen/actions)
[![Documentation](https://badgen.net/badge/icon/Docs?icon=wiki&label)](https://zigtools.github.io/zig-lsp-codegen)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

Zig LSP codegen from the newly released, official metamodel! This actually good code replaces the much hackier [lsp-typegen](https://github.com/zigtools/lsp-typegen);
# Zig LSP Codegen

## Usage
Generates `std.json` compatible Zig code based on the official [LSP MetaModel](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#metaModel)

1. `git clone`
2. Plop `metaModel.json` in this cloned repo. A copy can be found [here](https://github.com/microsoft/vscode-languageserver-node/blob/main/protocol/metaModel.json).
3. `zig build`
4. Tada! You should now have a `zig-out/artifacts/lsp.zig` file that can be used to your heart's content! Enjoy :)
## Installation

```bash
# Initialize a `zig build` project if you haven't already
zig init
# Add the `lsp-codegen` package to your `build.zig.zon`
zig fetch --save=lsp-codegen git+https://github.com/zigtools/zig-lsp-codegen.git#<git-commit-hash>
```

You can then import `lsp-codegen` in your `build.zig` with:

```zig
const lsp_codegen = b.dependency("lsp-codegen", .{});
const exe = b.addExecutable(...);
exe.root_module.addImport("lsp", lsp_codegen.module("lsp"));
```
34 changes: 27 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,48 @@ pub fn build(b: *std.Build) void {
const optimize = b.standardOptimizeOption(.{});

const test_filters = b.option([]const []const u8, "test-filter", "Skip tests that do not match filter") orelse &[0][]const u8{};
const meta_model_path = b.option([]const u8, "meta-model", "Specify path to the metaModel.json") orelse "metaModel.json";

// -------------------------------------------------------------------------

const exe = b.addExecutable(.{
.name = "zig-lsp-codegen",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.target = b.graph.host,
});
b.installArtifact(exe);
// The metaMode.json file should be removed once https://github.com/ziglang/zig/issues/17895 has been resolved.
exe.root_module.addAnonymousImport("meta-model", .{ .root_source_file = b.path("metaModel.json") });

const run_codegen = b.addRunArtifact(exe);
run_codegen.addFileArg(.{ .cwd_relative = meta_model_path });
const lsp_output_file = run_codegen.addOutputFileArg("lsp.zig");

const install_lsp_artifact = b.addInstallFile(lsp_output_file, "artifacts/lsp.zig");
b.getInstallStep().dependOn(&install_lsp_artifact.step);

_ = b.addModule("lsp", .{
.root_source_file = lsp_output_file,
.target = target,
.optimize = optimize,
});

const install_lsp_artifact = b.addInstallFile(lsp_output_file, "artifacts/lsp.zig");
b.getInstallStep().dependOn(&install_lsp_artifact.step);
// -------------------------------- Autodoc --------------------------------

const autodoc_exe = b.addObject(.{
.name = "lsp",
.root_source_file = lsp_output_file,
.target = target,
.optimize = .Debug,
});

const install_docs = b.addInstallDirectory(.{
.source_dir = autodoc_exe.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "doc/lsp-codegen",
});

const docs_step = b.step("docs", "Generate and install documentation");
docs_step.dependOn(&install_docs.step);

// --------------------------------- Tests ---------------------------------

const tests = b.addTest(.{
.root_source_file = lsp_output_file,
Expand Down
Loading

0 comments on commit 94e3e8b

Please sign in to comment.