Skip to content
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

[Bash] Highlight ++, -- ( in arithmetic expression) and += as operator #197

Closed
zufuliu opened this issue Aug 13, 2023 · 3 comments
Closed
Labels
bash Caused by the bash lexer committed Issue fixed in repository but not in release

Comments

@zufuliu
Copy link
Contributor

zufuliu commented Aug 13, 2023

test case:

hello="hello, "
hello+=word
echo $hello

for ((i = 2; i > 0; i--)); do
   echo postfix dec $i
done
for ((i = 2; i > 0; --i)); do
   echo prefix dec $i
done
for ((i = 0; i < 2; i++)); do
   echo postfix inc $i
done
for ((i = 0; i < 2; ++i)); do
   echo prefix inc $i
done
@zufuliu zufuliu changed the title [Bash] Highlight ++ and -- as operator in arithmetic expression [Bash] Highlight ++, -- ( in arithmetic expression) and += as operator Aug 13, 2023
@nyamatongwe nyamatongwe added the bash Caused by the bash lexer label Aug 14, 2023
@nyamatongwe
Copy link
Member

This change uses SCE_SH_OPERATOR for ++ and -- in arithmetic expressions but does not improve += outside arithmetic expressions.

@@ -996,7 +996,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
 					}
 				}
 			} else if (setWordStart.Contains(sc.ch)) {
-				sc.SetState(SCE_SH_WORD | insideCommand);
+				sc.SetState(((cmdState == CmdState::Arithmetic) ? SCE_SH_IDENTIFIER : SCE_SH_WORD) | insideCommand);
 			} else if (sc.ch == '#') {
 				if (stylePrev != SCE_SH_WORD && stylePrev != SCE_SH_IDENTIFIER &&
 					(sc.currentPos == 0 || setMetaCharacter.Contains(sc.chPrev))) {
@@ -1045,7 +1045,11 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
 			} else if (sc.ch == '-' && // test operator or short and long option
 					   (IsUpperOrLowerCase(sc.chNext) || sc.chNext == '-') &&
 					   IsASpace(sc.chPrev)) {
-				sc.SetState(SCE_SH_WORD | insideCommand);
+				if (cmdState == CmdState::Arithmetic && sc.ch == '-') {
+					sc.SetState(SCE_SH_OPERATOR | insideCommand);
+				} else {
+					sc.SetState(SCE_SH_WORD | insideCommand);
+				}
 				sc.Forward();
 			} else if (setBashOperator.Contains(sc.ch)) {
 				bool isCmdDelim = false;

@zufuliu
Copy link
Contributor Author

zufuliu commented Aug 17, 2023

Try following patch?
bash-operator.patch

diff --git a/lexers/LexBash.cxx b/lexers/LexBash.cxx
index d9506690..aab2d7a7 100644
--- a/lexers/LexBash.cxx
+++ b/lexers/LexBash.cxx
@@ -647,7 +647,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
 				break;
 			case SCE_SH_WORD:
 				// "." never used in Bash variable names but used in file names
-				if (!setWord.Contains(sc.ch)) {
+				if (!setWord.Contains(sc.ch) || sc.Match('+', '=')) {
 					char s[500];
 					sc.GetCurrent(s, sizeof(s));
 					int identifierStyle = SCE_SH_IDENTIFIER | insideCommand;
@@ -996,7 +996,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
 					}
 				}
 			} else if (setWordStart.Contains(sc.ch)) {
-				sc.SetState(SCE_SH_WORD | insideCommand);
+				sc.SetState(((cmdState == CmdState::Arithmetic)? SCE_SH_IDENTIFIER : SCE_SH_WORD) | insideCommand);
 			} else if (sc.ch == '#') {
 				if (stylePrev != SCE_SH_WORD && stylePrev != SCE_SH_IDENTIFIER &&
 					(sc.currentPos == 0 || setMetaCharacter.Contains(sc.chPrev))) {
@@ -1043,6 +1043,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
 					HereDoc.Indent = false;
 				}
 			} else if (sc.ch == '-' && // test operator or short and long option
+					   cmdState != CmdState::Arithmetic &&
 					   (IsUpperOrLowerCase(sc.chNext) || sc.chNext == '-') &&
 					   IsASpace(sc.chPrev)) {
 				sc.SetState(SCE_SH_WORD | insideCommand);

nyamatongwe pushed a commit that referenced this issue Aug 17, 2023
@nyamatongwe nyamatongwe added the committed Issue fixed in repository but not in release label Aug 17, 2023
@zufuliu
Copy link
Contributor Author

zufuliu commented Sep 6, 2023

Similar to issue #203, arithmetic expression can span multilines without line continuation.

echo $((2
- 1))
echo $[2
- 1]
for ((i = 0; i < 2;
	i++)); do
	echo postfix inc $i
done
for ((i = 1; i < 4;
	i<<=1)); do
	echo postfix inc $i
done

bash-operator-0906.patch

diff --git a/lexers/LexBash.cxx b/lexers/LexBash.cxx
index 647cc262..47e4a54a 100644
--- a/lexers/LexBash.cxx
+++ b/lexers/LexBash.cxx
@@ -662,7 +662,7 @@ void SCI_METHOD LexerBash::Lex(Sci_PositionU startPos, Sci_Position length, int
 		// handle line continuation, updates per-line stored state
 		if (sc.atLineStart) {
 			CmdState state = CmdState::Body;	// force backtrack while retaining cmdState
-			if (!StyleForceBacktrack(MaskCommand(sc.state))) {
+			if (!StyleForceBacktrack(MaskCommand(sc.state)) && !AnyOf(cmdState, CmdState::Arithmetic)) {
 				if (!QuoteStack.lineContinuation) {	// retain last line's state
 					cmdState = CmdState::Start;
 				}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bash Caused by the bash lexer committed Issue fixed in repository but not in release
Projects
None yet
Development

No branches or pull requests

2 participants