-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
add std/syntaxes to hint syntax highlighters about string literals #17722
Conversation
This should be under |
fdeba75
to
d416f34
Compare
That would defeat the whole point; tools is mostly for standalone binaries, not for reusable library code; tests is not importable by users. The point is to allow users to syntax highlight string literals in nim files, using simple and explicit annotation that a syntax highlighter (sublime text, github) can exploit. This is common if you're doing ffi and need emit statements (c, cpp, js, asm) or even need to generate code in other languages from nim (eg python). We can bikeshed the module name though, if you have a better name in mind. Other candidates:
notethis will avoid bugs like this: because in when defined(js):
var p: cstring
asm """`p` = `Data`;"""
else:
var p = cast[cstring](data) => when defined(js):
var p: cstring
asm """`p` = `Data`;
"""
else:
var p = cast[cstring](data) |
I don't understand. Can't the syntax highlighter simply stick to the spec and render multi line string literals as string literals? |
About the name ... we need to distinguish between "std" which should be reserved for core libraries and "extensions" which are simply kinda there because we like the monorepo. |
after this PR, we can make syntax highlighters stick to the spec and render Note that tooling beyond syntax highlighters can exploit this (eg linters etc).
I don't have an objection with moving this file to Note that std and stdx should be able to mutually import each other, otherwise this again defeats purpose. stdx in other langs(these are not necessarily official, but it doesn't matter)
in |
Ok, let's use "stdx" then. The module's name could be "language_annotations" or maybe "lang_prefixes". "syntaxes" is not clear enough. |
Oh, I thought all this was is a test file for highlighters :D IMO this is not worth it, highlighters can just grow some heuristics instead or just display it as a plain string literal. |
Oh god no... These heuristics are always terrible. |
Agreed, but this isn't gonna improve anything realistically, since most users won't be aware that these hinting templates exist, and thus won't use them. This becomes even more true when we put it into some stdlib extension thing. |
So what, we can use them ourselves and others will pick it up or not. |
However, this must not turn into another distros.nim fiasco. There are so many PLs and markup languages around. We need to restrict it from the beginning to what |
It should probably be |
We could also provide a var code = highlighted"""```someLanguage
...
```""" And we would only need highlighters to parse a highlighted""" string literal as markdown, would support all languages supported by the editors markdown highlighter, and would not need per-language support from the Nim side |
d416f34
to
50538a9
Compare
piggybacking on @Clyybber's idea, here's my latest proposal:
proc highlightImpl(a: string): string =
for i in 0..<a.len:
if a[i] == '\n':
return a[i+1..a.len-1]
template highlight*(a: string{lit}): string =
const b = highlightImpl(a)
b
runnableExamples:
let a = highlight"""js
if (!Math.trunc) {
Math.trunc = function(v) {
v = +v;
if (!isFinite(v)) return v;
return (v - v % 1) || (v < 0 ? -0 : v === 0 ? v : 0);
};
}"""
assert a == """
if (!Math.trunc) {
Math.trunc = function(v) {
v = +v;
if (!isFinite(v)) return v;
return (v - v % 1) || (v < 0 ? -0 : v === 0 ? v : 0);
};
}""" now all we need is to "recommend" a few custom languages but we don't need symbols for it, so users can use
if they want, nim doesn't need to know about this. (it's simpler than
) I will update this PR soon other candidate names after this change
Regarding stdx, well, we have to start somewhere... see stdx/readme.md in this PR; even if this module belongs in std/ rather than stdx |
Awesome! (only remaining question is if it's as easy to make it support a few languages as with the markdown embedding, but I guess the respective markdown highlighter has to do that too, so should be possible)
@Araq and I had a discussion on IRC, resulting in: we can put it in |
Er ... I didn't agree. Don't put it in system.nim. System.nim code can import other modules anyway these days. |
5ebf782
to
49e5225
Compare
This pull request has been automatically marked as stale because it has not had recent activity. If you think it is still a valid PR, please rebase it on the latest devel; otherwise it will be closed. Thank you for your contributions. |
this PR allows highlighting hints
with the following additions to Syntaxes/Nim.YAML-tmLanguage (https://github.com/Varriount/NimLime, also used for github syntax highlight via github linguist):
https://gist.github.com/timotheecour/192fcbb8fda23b2012f8f420d88bf893
the std/syntaxes module renders as follows:
future work
asm lang"js ..."