Add itm "Iterate keys and values of java.util.Map" default Java Live Template

also to demonstrate typeParameterOf() macro

GitOrigin-RevId: ba5fede72fc9391bfa4e97426b0cd706c429fa1d
This commit is contained in:
Bas Leijdekkers
2022-10-12 18:41:44 +00:00
committed by intellij-monorepo-bot
parent 2786f8bf0d
commit 67dbbe7dce
5 changed files with 46 additions and 2 deletions
@@ -0,0 +1,14 @@
import java.util.*;
class BarGoo {}
class Foo {
{
Map<String, BarGoo> goos;
for (Map.Entry<String, BarGoo> stringBarGooEntry : goos.entrySet()) {
String key = stringBarGooEntry.getKey();
BarGoo value = stringBarGooEntry.getValue();
<caret>
}
}
}
@@ -0,0 +1,10 @@
import java.util.*;
class BarGoo {}
class Foo {
{
Map<String, BarGoo> goos;
<caret>
}
}
@@ -221,7 +221,7 @@ class Outer {
private void stripTrailingSpaces() {
DocumentImpl document = (DocumentImpl)getEditor().getDocument()
document.setStripTrailingSpacesEnabled(true)
document.stripTrailingSpaces(getProject())
document.stripTrailingSpaces(getProject())
PsiDocumentManager.getInstance(getProject()).commitAllDocuments()
}
@@ -243,6 +243,13 @@ class Outer {
startTemplate("soutp", "Java")
checkResult()
}
void testItm() {
configure()
startTemplate("itm", "Java")
WriteCommandAction.runWriteCommandAction(project) { state.gotoEnd(false) }
checkResult()
}
void testSoutConsumerApplicability() {
for (String name : ["soutc", "serrc"]) {
@@ -507,7 +514,7 @@ class Foo {
'''
}
void "test ritar template in expression lambda"() {
void "test itar template in expression lambda"() {
myFixture.configureByText 'a.java', '''class Foo {
void test(int[] arr) {
Runnable r = () -> itar<caret>
@@ -179,6 +179,7 @@ livetemplate.description.itco=Iterate elements of java.util.Collection
livetemplate.description.iten=Iterate java.util.Enumeration
livetemplate.description.itit=Iterate java.util.Iterator
livetemplate.description.itli=Iterate elements of java.util.List
livetemplate.description.itm=Iterate keys and values of java.util.Map
livetemplate.description.ittok=Iterate tokens from String
livetemplate.description.ritar=Iterate elements of array in reverse order
livetemplate.description.iter=Iterate Iterable or array
+12
View File
@@ -71,6 +71,18 @@
<option name="JAVA_STATEMENT" value="true" />
</context>
</template>
<template name="itm" value="for (java.util.Map.Entry&lt;$KEY_TYPE$, $VALUE_TYPE$&gt; $ENTRY$: $MAP$.entrySet()) {&#10; $KEY_TYPE$ $KEY$ = $ENTRY$.getKey();&#10; $VALUE_TYPE$ $VALUE$ = $ENTRY$.getValue();&#10; $END$&#10;}"
resource-bundle="messages.CodeInsightBundle" key="livetemplate.description.itm" toReformat="true" toShortenFQNames="true">
<variable name="MAP" expression="variableOfType(&quot;java.util.Map&quot;)" defaultValue="&quot;map&quot;" alwaysStopAt="true" />
<variable name="KEY_TYPE" expression="typeParameterOf(MAP,&quot;0&quot;)" defaultValue="" alwaysStopAt="false" />
<variable name="VALUE_TYPE" expression="typeParameterOf(MAP,&quot;1&quot;)" defaultValue="" alwaysStopAt="false" />
<variable name="ENTRY" expression="suggestVariableName()" defaultValue="&quot;entry&quot;" alwaysStopAt="true" />
<variable name="KEY" expression="suggestVariableName()" defaultValue="&quot;key&quot;" alwaysStopAt="true" />
<variable name="VALUE" expression="suggestVariableName()" defaultValue="&quot;value&quot;" alwaysStopAt="true" />
<context>
<option name="JAVA_STATEMENT" value="true" />
</context>
</template>
<template name="ittok" value="for (java.util.StringTokenizer $TOKENIZER$ = new java.util.StringTokenizer($STRING$); $TOKENIZER$.hasMoreTokens(); ) {&#10; String $VAR$ = $TOKENIZER_COPY$.nextToken();&#10; $END$&#10;}&#10;"
resource-bundle="messages.CodeInsightBundle" key="livetemplate.description.ittok" toReformat="true" toShortenFQNames="true">
<variable name="TOKENIZER" expression="suggestVariableName()" defaultValue="&quot;tokenizer&quot;" alwaysStopAt="true" />