1
0
mirror of https://github.com/janet-lang/janet synced 2025-06-19 15:04:13 +00:00

Add code in parser to automatically indent long strings.

Leading spaces are stripped based on the column index of the first
backtick character in the first delimiter. If there are
characters that are not newline or space before that column in the
string, then the behavior is the same as the old behvaior - no
re-indentation is performed.
This commit is contained in:
Calvin Rose 2020-11-26 13:20:58 -06:00
parent 451340e4c0
commit 7fd2da1096
2 changed files with 58 additions and 19 deletions

View File

@ -2842,21 +2842,23 @@
{"h" (fn [&] {"h" (fn [&]
(print "usage: " (dyn :executable "janet") " [options] script args...") (print "usage: " (dyn :executable "janet") " [options] script args...")
(print (print
`Options are: ```
-h : Show this help Options are:
-v : Print the version string -h : Show this help
-s : Use raw stdin instead of getline like functionality -v : Print the version string
-e code : Execute a string of janet -s : Use raw stdin instead of getline like functionality
-d : Set the debug flag in the REPL -e code : Execute a string of janet
-r : Enter the REPL after running all scripts -d : Set the debug flag in the REPL
-p : Keep on executing if there is a top-level error (persistent) -r : Enter the REPL after running all scripts
-q : Hide logo (quiet) -p : Keep on executing if there is a top-level error (persistent)
-k : Compile scripts but do not execute (flycheck) -q : Hide logo (quiet)
-m syspath : Set system path for loading global modules -k : Compile scripts but do not execute (flycheck)
-c source output : Compile janet source code into an image -m syspath : Set system path for loading global modules
-n : Disable ANSI color output in the REPL -c source output : Compile janet source code into an image
-l lib : Import a module before processing more arguments -n : Disable ANSI color output in the REPL
-- : Stop handling options`) -l lib : Import a module before processing more arguments
-- : Stop handling options
```)
(os/exit 0) (os/exit 0)
1) 1)
"v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1) "v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1)

View File

@ -313,11 +313,48 @@ static int stringend(JanetParser *p, JanetParseState *state) {
uint8_t *bufstart = p->buf; uint8_t *bufstart = p->buf;
int32_t buflen = (int32_t) p->bufcount; int32_t buflen = (int32_t) p->bufcount;
if (state->flags & PFLAG_LONGSTRING) { if (state->flags & PFLAG_LONGSTRING) {
/* Check for leading newline character so we can remove it */ /* Post process to remove leading whitespace */
if (bufstart[0] == '\n') { JanetParseState top = p->states[p->statecount - 1];
bufstart++; int32_t indent_col = (int32_t) top.column - 1;
buflen--; uint8_t *r = bufstart, *end = r + buflen;
/* Check if there are any characters before the start column -
* if so, do not reindent. */
int reindent = 1;
while (reindent && (r < end)) {
if (*r++ == '\n') {
for (int32_t j = 0; (r < end) && (*r != '\n') && (j < indent_col); j++, r++) {
if (*r != ' ') {
reindent = 0;
break;
}
}
}
} }
/* Now reindent if able to, otherwise just drop leading newline. */
if (!reindent) {
if (buflen > 0 && bufstart[0] == '\n') {
buflen--;
bufstart++;
}
} else {
uint8_t *w = bufstart;
r = bufstart;
while (r < end) {
if (*r == '\n') {
if (r == bufstart) {
/* Skip leading newline */
r++;
} else {
*w++ = *r++;
}
for (int32_t j = 0; (r < end) && (*r != '\n') && (j < indent_col); j++, r++);
} else {
*w++ = *r++;
}
}
buflen = (int32_t) (w - bufstart);
}
/* Check for trailing newline character so we can remove it */
if (buflen > 0 && bufstart[buflen - 1] == '\n') { if (buflen > 0 && bufstart[buflen - 1] == '\n') {
buflen--; buflen--;
} }