Skip to content

Commit

Permalink
Added try branch for java (#2346)
Browse files Browse the repository at this point in the history
* Added branch scope for try catch statements in java
* Added missing iteration scope for if else branch in java

## Checklist

- [x] I have added
[tests](https://www.cursorless.org/docs/contributing/test-ca/e-recorder/)
- [/] I have updated the
[docs](https://github.com/cursorless-dev/cursorless/tree/main/docs) and
[cheatsheet](https://github.com/cursorless-dev/cursorless/tree/main/cursorless-talon/src/cheatsheet)
- [/] I have not broken the cheatsheet
  • Loading branch information
AndreasArvidsson authored Jun 14, 2024
1 parent b8a2f42 commit d0a2b2a
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 0 deletions.
12 changes: 12 additions & 0 deletions data/fixtures/scopes/java/branch.if.iteration.scope
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if (true) {}
else if (false) {}
else {}
---

[Range] =
[Domain] = 0:0-2:7
>------------
0| if (true) {}
1| else if (false) {}
2| else {}
-------<
35 changes: 35 additions & 0 deletions data/fixtures/scopes/java/branch.if.scope
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
if (true) {}
else if (false) {}
else {}
---

[#1 Content] =
[#1 Domain] = 0:0-0:12
>------------<
0| if (true) {}

[#1 Removal] = 0:0-1:5
>------------
0| if (true) {}
1| else if (false) {}
-----<

[#1 Insertion delimiter] = "\n"


[#2 Content] =
[#2 Removal] =
[#2 Domain] = 1:0-1:18
>------------------<
1| else if (false) {}

[#2 Insertion delimiter] = "\n"


[#3 Content] =
[#3 Removal] =
[#3 Domain] = 2:0-2:7
>-------<
2| else {}

[#3 Insertion delimiter] = "\n"
12 changes: 12 additions & 0 deletions data/fixtures/scopes/java/branch.try.iteration.scope
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
try {}
catch(Exception e) {}
finally {}
---

[Range] =
[Domain] = 0:0-2:10
>------
0| try {}
1| catch(Exception e) {}
2| finally {}
----------<
30 changes: 30 additions & 0 deletions data/fixtures/scopes/java/branch.try.scope
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
try {}
catch(Exception e) {}
finally {}
---

[#1 Content] =
[#1 Removal] =
[#1 Domain] = 0:0-0:6
>------<
0| try {}

[#1 Insertion delimiter] = "\n"


[#2 Content] =
[#2 Removal] =
[#2 Domain] = 1:0-1:21
>---------------------<
1| catch(Exception e) {}

[#2 Insertion delimiter] = "\n"


[#3 Content] =
[#3 Removal] =
[#3 Domain] = 2:0-2:10
>----------<
2| finally {}

[#3 Insertion delimiter] = "\n"
5 changes: 5 additions & 0 deletions packages/common/src/scopeSupportFacets/java.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ export const javaScopeSupport: LanguageScopeSupportFacetMap = {
attribute: notApplicable,
"key.attribute": notApplicable,
"value.attribute": notApplicable,

"branch.if": supported,
"branch.if.iteration": supported,
"branch.try": supported,
"branch.try.iteration": supported,
};
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ export const scopeSupportFacetInfos: Record<
description: "A try/catch/finally branch",
scopeType: "branch",
},
"branch.try.iteration": {
description:
"Iteration scope for try/catch/finally branch; should be the entire try-catch statement",
scopeType: "branch",
isIteration: true,
},
"branch.switchCase": {
description: "A case/default branch in a switch/match statement",
scopeType: "branch",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const scopeSupportFacets = [
"branch.if",
"branch.if.iteration",
"branch.try",
"branch.try.iteration",
"branch.switchCase",
"branch.switchCase.iteration",
"branch.ternary",
Expand Down
22 changes: 22 additions & 0 deletions queries/java.scm
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,28 @@
alternative: (block) @branch.end
)

(
(if_statement) @branch.iteration
(#not-parent-type? @branch.iteration "if_statement")
)

;;!! try {}
;;! ^^^^^^
(try_statement
"try" @branch.start
body: (_) @branch.end
)

;;!! catch (Exception e) {}
;;! ^^^^^^^^^^^^^^^^^^^^^^
(catch_clause) @branch

;;!! finally {}
;;! ^^^^^^^^^^
(finally_clause) @branch

(try_statement) @branch.iteration

;;!! for (int i = 0; i < 5; ++i) {}
;;! ^^^^^
;;! ------------------------------
Expand Down

0 comments on commit d0a2b2a

Please sign in to comment.