ziglang / zig-mode Goto Github PK
View Code? Open in Web Editor NEWZig mode for Emacs
License: GNU General Public License v3.0
Zig mode for Emacs
License: GNU General Public License v3.0
It nice if this mode supported formant on save like the vim plugin dose
When an error occurst in zig fmt
the compilation buffer doesn't interpret the ASNI colour codes in the output, and looks kind of ugly (and somewhat hard to read) as a result.
There should be a way to configure this change:
diff --git a/zig-mode.el b/zig-mode.el
index de7ff92..6adca7d 100644
--- a/zig-mode.el
+++ b/zig-mode.el
@@ -110,7 +110,7 @@ If given a SOURCE, execute the CMD on it."
(reformatter-define zig-format
:program zig-zig-bin
- :args '("fmt" "--stdin")
+ :args '("fmt" "--stdin" "--ast-check")
:group 'zig-mode
:lighter " ZigFmt")
First of all thank you so much for this package! ❤️
I just wrote my own function to save the current buffer when I execute zig-run
with C-x C-r
in doomemacs:
(use-package! zig-mode
:config
(defun my/save-buffer-and-execute-zig-run ()
"Save the current buffer and execute `zig-run`."
(interactive)
(save-buffer)
(zig-run))
(define-key zig-mode-map (kbd "C-c C-r") #'my/save-buffer-and-execute-zig-run))
Without this most of the time I found myself pressing C-c C-r
only to realise that I haven't saved the file yet.
Should zig-run
always save the buffer when it is executed? I'd be happy to send in a PR but wanted to check first before I sent one.
Sometimes, the following would happen on save and result in losses of undo-history.
It is determined that zig-format-on-save is the culprit, and setting it to nil can be a workaround.
buffer-undo-list is not empty. Do you want to recover now? (y or n) y
File digest doesn’t match, so undo history will be discarded.
Example, typing this:
const MultibootHeader = packed struct {
magic: usize,
flags: usize,
checksum: usize,
};
Let's say I have src/main.zig
with following:
const std = @import("std");
const zap = @import("zap");
...
I have zap
defined in build.zig
& build.zig.zon
, zig build
succeeds.
But when I try to zig-run
from Emacs it fails with:
main.zig:2:21: error: no package named 'zap' available within package 'root'
I probably know why it happens (sorry I am relatively new to Zig), because zig run
does not play well with build.zig
. But doesn't this make zig run
and zig-run
Emacs function useless for larger (and practical) projects?
How do you run your code from within Emacs while making projects with Zig?
Emacs c-mode
applies font-lock-constant-face
to labels (e.g. foo:
) and label references (e.g. goto foo;
). We should have zig-mode
do something similar for e.g. blk: { ... }
and break :blk
.
I don't have my config setup to format on save so I rely on the mode indentation a lot, and it currently will produce this:
fn sign(i: i32) i32 {
return if (i == 0)
0
else if (i > 0)
-1
else
-1;
}
While the preferred style according to zig fmt would be:
fn sign(i: i32) i32 {
return if (i == 0)
0
else if (i > 0)
-1
else
-1;
}
With Emacs 29.1 being released, Tree Sitter support is now natively in Emacs. However, to use it there needs to be a dedicated major mode extending the base mode. In this case there would need to be a zig-ts-mode. Would that be possible?
Is there any rationale for using release-fast
for zig-test-buffer
? Personally, I would think that testing should use the debug release mode by default to catch any illegal behaviour, since that's what testing should flag out.
zig-mode
currently exposes zig-run
, which creates an executable from the current buffer and runs it immediately.
However, for bigger projects, there is usually a buils.zig
with a run
step, which could be invoked via zig build run
.
It would be nice to have an ability to invoke that project-level run.
build.zig
is super common and having this improves developer experience, making edit-run-loop shorter.
run
step is a custom thing, which is not always available. We can have a defcustom
for it with a default value and then users can either configure it in their configs or dir-locals.
We can expose zig-project-run
(bound to C-c C-p
or something, have no strong preferences).
I can work on a PR for this.
Hello, since #51, zig-mode uses reformatter.el
for running the formatter.
One thing I noticed is that zig-mode depends on at least version 0.6 of the package:
;; Package-Requires: ((emacs "24.3") (reformatter "0.6"))
while the latest version of reformatter is 0 at present:
;; Package-Version: 0
Apparently reformatter.el has no version yet, but 0.6 is bigger than 0, so the latest version of reformatter.el is considered outdated.
Is there any reason @joachimschmidt557 specified 0.6 as the minimum version? Is it just a mistake? If so, I would send a PR to set the minimum version to 0.
Replacing this mode with a cc-mode derived mode would provide better integration with other emacs packages for c-like languages and allow users cc-mode configuration to work in zig-mode.
An example from std.os.windows, everything after the following line is highlighted as a string because it sees the end quote as escaped:
const prefix_u8 = "\\DosDevices\\";
I typed customize-mode
to add something to the hook, only to find that there wasn't one. Is there something I'm missing?
Here's a screencast to show what I mean by "make the cursor jump"
The new zig-mode failed to compile on older version like 26.3. The reason is that emacs 27's rx's regexp
and new literal
accept arbitrary lisp as arguments while older ones only accept string known at compile time.
So we need to either bump the minimal requirement to 27 or fix it (e.g. by wrapping the entire file inside a macro)
Zig version: 0.6.0+53c63bdb7
on Linux 5.8.14-arch1-1
Running M-x zig-test-buffer
returns
Compilation started at Tue Oct 13 16:52:15
zig test /home/username/hello/main-testing.zig --release-fast
error: unrecognized parameter: '--release-fast'
Compilation exited abnormally with code 1 at Tue Oct 13 16:52:15
because looks like at some point flags --release-*
were replaced with an option -O [mode]
$ zig test --help
...
-O [mode] Choose what to optimize for
Debug (default) Optimizations off, safety on
ReleaseFast Optimizations on, safety off
ReleaseSafe Optimizations on, safety on
ReleaseSmall Optimize for small binary, safety off
...
When I have zig-mode
enabled for a buffer and save that buffer while lsp-mode
is enabled, LSP seems to turn off (the icon disappears from the modeline, and completions no longer work).
Distribution: Debian GNU/Linux bullseye/testing
Emacs version: 27.1
Doom Emacs version: doomemacs/doomemacs@f7293fb6
zig-mode version: 20201022.955
https://github.com/purcell/reformatter.el provides an easy interface to provide idiomatic formatting commands. Using this library would make it would be possible to move a lot of the code which deals with zig fmt
from this package to reformatter.el
. Of course, this means that this mode has one more Elisp package as a dependency.
I use text-scale-increase to change my font size.
But every time I save a zig file, my font size gets reset...
When trying to write:
const std = @import("std");
pub fn main() void
{
std.debug.warn("hello");
}
The auto indenter turns it into:
const std = @import("std");
pub fn main() void
{
std.debug.warn("hello");
}
Could you please specify the license used by this package?
Assuming that you want to release under "the GPL v3 or any later version", the best way to do that would be to add this to the library header:
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
You could also, or instead, add a LICENSE
file containing the text of the GPL-3. But if you only add that file, then the "or (at your option) any later version" bit won't be known. I recommend that you do both.
If this is too noisy for you, then you could also add one of these following lines to the header. (I do however recommend against doing only that - a judge might decide that this is not sufficient.)
;; License: GNU General Public License version 3, or (at your option) any later version
;; License: GNU General Public License version 3, or any later version
;; License: GNU GPL version 3, or (at your option) any later version
;; License: GPL version 3, or (at your option) any later version
;; License: GPL version 3, or any later version
;; License: GPL v3, or (at your option) any later version
;; License: GPL v3, or any later version
;; License: GPL-3+
Someone on Discord gave me this code that allows flycheck
to work with zig-mode
:
(flycheck-define-checker zig
"A zig syntax checker using the zig-fmt interpreter."
:command ("zig" "fmt" (eval (buffer-file-name)))
:error-patterns
((error line-start (file-name) ":" line ":" column ": error: " (message) line-end))
:modes zig-mode)
(add-to-list 'flycheck-checkers 'zig)
And both flycheck
and lsp-ui
worked:
However, zig-mode
always opens *zig-fmt*
split window. This is duplicate, so it's nice if we can suppress the behavior.
Thank you.
First of all, thanks for this awesome package! 🚀
I wonder does this package needs more maintainers? 🤔If so, I would like volunteer myself. I had this question because there were trivial issues, and PRs were left unresponsed.
I serve as the primary maintainer for emacs-dashboard and one of the maintainers for lsp-mode (focus on contributing to lsp-zig.el
). I have developed over a hundred Emacs packages. I hope I am qualified to help maintain this package.
Cheers!
Got an ugly error message when trying to load zig-mode:
File mode specification error: (file-error Cannot open load file Not a directory reformatter)
Turns out there is a dependency on emacs-reformatter. Only figured it out because I tried to run the tests and I noticed the script installed reformatter using elpa.
It would be helpful if the README could mention adding emacs-reformatter's reformatter.el to load-path before trying to load zig-mode.
Whenever I save and it reformats my code, I need to manually revert the buffer -- otherwise I get told that the file I'm editing has been modified.
M-x zig-mode won't switch to zig-mode, it returns error instead: "rx form 'regexp' requires args satisfying `stringp'"
emacs-version: 24.5.1, package: 20201013.1249
Hey thanks for this! I start out with:
//Hey this is a really really really really really really really long line.
And run fill-region
on the line and get:
//Hey this is a really really really really really really really
long line.
The subsequent lines are incorrectly not commented.
Or another variation of it (before fill-region
):
//Hey this is a really really really really really really really really really really
//long line.
And after fill-region:
//Hey this is a really really really really really really really
really really really //long line.
It doesn't preserve the comment markers in the right place.
It would also be great if this was supported for multiline strings. Emacs does the wrong thing in zig-mode if I have:
\\Hey this is a really really really really really really really really really really
\\long line.
And I call fill-region
on that:
\\Hey this is a really really really really really really really
really really really \\long line.
I'd hope that it preserves the string like I'd hope it preserves the comment.
In bash-mode if you do fill-region on a commented section, it fills the region keeping the comment prefix correctly.
(before fill-region)
#Hey this is a really really really really really really really really really really
#long line.
(after fill-region)
#Hey this is a really really really really really really really really
#really really long line.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.