Highlight simple f-string interpolations

This commit is contained in:
K Lange 2021-03-26 23:32:58 +09:00
parent 551b5d8f2e
commit 57970d662e

75
src/vendor/rline.c vendored
View File

@ -523,7 +523,42 @@ void paintNHex(struct syntax_state * state, int n) {
} }
} }
void paint_krk_string(struct syntax_state * state, int type) { void paint_krk_string_shared(struct syntax_state * state, int type, int isFormat) {
if (charat() == '\\') {
if (nextchar() == 'x') {
paintNHex(state, 2);
} else if (nextchar() == 'u') {
paintNHex(state, 4);
} else if (nextchar() == 'U') {
paintNHex(state, 8);
} else if (nextchar() >= '0' && nextchar() <= '7') {
paint(2, FLAG_ESCAPE);
if (charat() >= '0' && charat() <= '7') {
paint(1, FLAG_ESCAPE);
if (charat() >= '0' && charat() <= '7') {
paint(1, FLAG_ESCAPE);
}
}
} else {
paint(2, FLAG_ESCAPE);
}
} else if (isFormat && charat() == '{') {
paint(1, FLAG_NUMERAL);
if (charat() == '}') {
state->i--;
paint(2, FLAG_ERROR); /* Can't do that. */
} else {
while (charat() != -1 && charat() != '}') {
paint(1, FLAG_NUMERAL);
}
paint(1, FLAG_NUMERAL);
}
} else {
paint(1, FLAG_STRING);
}
}
void paint_krk_string(struct syntax_state * state, int type, int isFormat) {
/* Assumes you came in from a check of charat() == '"' */ /* Assumes you came in from a check of charat() == '"' */
paint(1, FLAG_STRING); paint(1, FLAG_STRING);
while (charat() != -1) { while (charat() != -1) {
@ -532,26 +567,8 @@ void paint_krk_string(struct syntax_state * state, int type) {
} else if (charat() == type) { } else if (charat() == type) {
paint(1, FLAG_STRING); paint(1, FLAG_STRING);
return; return;
} else if (charat() == '\\') {
if (nextchar() == 'x') {
paintNHex(state, 2);
} else if (nextchar() == 'u') {
paintNHex(state, 4);
} else if (nextchar() == 'U') {
paintNHex(state, 8);
} else if (nextchar() >= '0' && nextchar() <= '7') {
paint(2, FLAG_ESCAPE);
if (charat() >= '0' && charat() <= '7') {
paint(1, FLAG_ESCAPE);
if (charat() >= '0' && charat() <= '7') {
paint(1, FLAG_ESCAPE);
}
}
} else {
paint(2, FLAG_ESCAPE);
}
} else { } else {
paint(1, FLAG_STRING); paint_krk_string_shared(state,type,isFormat);
} }
} }
} }
@ -610,16 +627,18 @@ int paint_krk_numeral(struct syntax_state * state) {
return 0; return 0;
} }
int paint_krk_triple_string(struct syntax_state * state, int type) { int paint_krk_triple_string(struct syntax_state * state, int type, int isFormat) {
while (charat() != -1) { while (charat() != -1) {
if (charat() == type) { if (charat() == '\\' && nextchar() == type) {
paint(2, FLAG_ESCAPE);
} else if (charat() == type) {
paint(1, FLAG_STRING); paint(1, FLAG_STRING);
if (charat() == type && nextchar() == type) { if (charat() == type && nextchar() == type) {
paint(2, FLAG_STRING); paint(2, FLAG_STRING);
return 0; return 0;
} }
} else { } else {
paint(1, FLAG_STRING); paint_krk_string_shared(state,type,isFormat);
} }
} }
return (type == '"') ? 1 : 2; /* continues */ return (type == '"') ? 1 : 2; /* continues */
@ -636,12 +655,13 @@ int syn_krk_calculate(struct syntax_state * state) {
while (c_keyword_qualifier(charat())) paint(1, FLAG_TYPE); while (c_keyword_qualifier(charat())) paint(1, FLAG_TYPE);
return 0; return 0;
} else if (charat() == '"' || charat() == '\'') { } else if (charat() == '"' || charat() == '\'') {
int isFormat = (lastchar() == 'f');
if (nextchar() == charat() && charrel(2) == charat()) { if (nextchar() == charat() && charrel(2) == charat()) {
int type = charat(); int type = charat();
paint(3, FLAG_STRING); paint(3, FLAG_STRING);
return paint_krk_triple_string(state, type); return paint_krk_triple_string(state, type, isFormat);
} else { } else {
paint_krk_string(state, charat()); paint_krk_string(state, charat(), isFormat);
} }
return 0; return 0;
} else if (find_keywords(state, syn_krk_keywords, FLAG_KEYWORD, c_keyword_qualifier)) { } else if (find_keywords(state, syn_krk_keywords, FLAG_KEYWORD, c_keyword_qualifier)) {
@ -660,10 +680,11 @@ int syn_krk_calculate(struct syntax_state * state) {
return 0; return 0;
} }
break; break;
/* rline doesn't support multiline editing anyway */
case 1: case 1:
return paint_krk_triple_string(state, '"'); return paint_krk_triple_string(state, '"', 0);
case 2: case 2:
return paint_krk_triple_string(state, '\''); return paint_krk_triple_string(state, '\'', 0);
} }
return -1; return -1;
} }