-
Notifications
You must be signed in to change notification settings - Fork 874
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement var type information display as tooltip
- Loading branch information
1 parent
bb3bdff
commit 60fb7d5
Showing
13 changed files
with
296 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...enfiles/org/netbeans/modules/java/completion/JavaTooltipTaskTest/17/VarSampleBounds1.pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Query offset: 901 | ||
Anchor offset: 0 | ||
Tooltip offset: 0 | ||
Tooltip index: 0 | ||
|
10 changes: 10 additions & 0 deletions
10
...enfiles/org/netbeans/modules/java/completion/JavaTooltipTaskTest/17/VarSampleBounds2.pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Query offset: 902 | ||
Anchor offset: 902 | ||
Tooltip offset: 902 | ||
Tooltip index: 0 | ||
|
||
[ | ||
[ | ||
Iterator<String> | ||
[ | ||
] |
10 changes: 10 additions & 0 deletions
10
...enfiles/org/netbeans/modules/java/completion/JavaTooltipTaskTest/17/VarSampleBounds3.pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Query offset: 903 | ||
Anchor offset: 902 | ||
Tooltip offset: 902 | ||
Tooltip index: 0 | ||
|
||
[ | ||
[ | ||
Iterator<String> | ||
[ | ||
] |
10 changes: 10 additions & 0 deletions
10
...enfiles/org/netbeans/modules/java/completion/JavaTooltipTaskTest/17/VarSampleBounds4.pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Query offset: 905 | ||
Anchor offset: 902 | ||
Tooltip offset: 902 | ||
Tooltip index: 0 | ||
|
||
[ | ||
[ | ||
Iterator<String> | ||
[ | ||
] |
5 changes: 5 additions & 0 deletions
5
...enfiles/org/netbeans/modules/java/completion/JavaTooltipTaskTest/17/VarSampleBounds5.pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Query offset: 906 | ||
Anchor offset: 0 | ||
Tooltip offset: 0 | ||
Tooltip index: 0 | ||
|
10 changes: 10 additions & 0 deletions
10
...ldenfiles/org/netbeans/modules/java/completion/JavaTooltipTaskTest/17/VarSampleType1.pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Query offset: 1063 | ||
Anchor offset: 1062 | ||
Tooltip offset: 1062 | ||
Tooltip index: 0 | ||
|
||
[ | ||
[ | ||
String | ||
[ | ||
] |
10 changes: 10 additions & 0 deletions
10
...ldenfiles/org/netbeans/modules/java/completion/JavaTooltipTaskTest/17/VarSampleType2.pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Query offset: 1088 | ||
Anchor offset: 1086 | ||
Tooltip offset: 1086 | ||
Tooltip index: 0 | ||
|
||
[ | ||
[ | ||
X<Double> | ||
[ | ||
] |
10 changes: 10 additions & 0 deletions
10
...ldenfiles/org/netbeans/modules/java/completion/JavaTooltipTaskTest/17/VarSampleType3.pass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
Query offset: 1172 | ||
Anchor offset: 1172 | ||
Tooltip offset: 1172 | ||
Tooltip index: 0 | ||
|
||
[ | ||
[ | ||
X1 & X2 | ||
[ | ||
] |
60 changes: 60 additions & 0 deletions
60
java/java.completion/test/unit/data/org/netbeans/modules/java/completion/data/VarSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package test; | ||
|
||
public class Test { | ||
|
||
private void test(java.util.List<String> l) { | ||
var v1 = l.iterator(); | ||
} | ||
|
||
public static class X<T> { | ||
|
||
public static void main(String[] args) { | ||
X<String> x = new X<>(); | ||
var a = ""; | ||
var y = x.run1() | ||
.run2() | ||
.run3(); | ||
var z = x.x("Hallo", 42); | ||
} | ||
|
||
public <X, Y, Z extends X1 & X2> Z x(Y y, X x) { | ||
return null; | ||
} | ||
|
||
public X<Integer> run1() { | ||
return null; | ||
} | ||
|
||
public X<String> run2() { | ||
return null; | ||
} | ||
|
||
public X<Double> run3() { | ||
return null; | ||
} | ||
|
||
interface X1 { | ||
} | ||
|
||
interface X2 { | ||
} | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
...va.completion/test/unit/src/org/netbeans/modules/java/completion/JavaTooltipTaskTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.netbeans.modules.java.completion; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.StringWriter; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import javax.swing.text.Document; | ||
import org.netbeans.api.java.lexer.JavaTokenId; | ||
import org.netbeans.api.lexer.Language; | ||
import org.netbeans.modules.parsing.api.ParserManager; | ||
import org.netbeans.modules.parsing.api.Source; | ||
import org.openide.cookies.EditorCookie; | ||
import org.openide.filesystems.FileObject; | ||
import org.openide.filesystems.FileUtil; | ||
import org.openide.loaders.DataObject; | ||
|
||
import static junit.framework.TestCase.assertNotNull; | ||
|
||
public class JavaTooltipTaskTest extends CompletionTestBaseBase { | ||
|
||
public JavaTooltipTaskTest(String testName) { | ||
super(testName, "org/netbeans/modules/java/completion/JavaTooltipTaskTest"); | ||
} | ||
|
||
public void testVarBounds() throws Exception { | ||
assertResult("17", "VarSample", "VarSampleBounds1.pass", 901); | ||
assertResult("17", "VarSample", "VarSampleBounds2.pass", 902); | ||
assertResult("17", "VarSample", "VarSampleBounds3.pass", 903); | ||
assertResult("17", "VarSample", "VarSampleBounds4.pass", 905); | ||
assertResult("17", "VarSample", "VarSampleBounds5.pass", 906); | ||
} | ||
|
||
public void testVarTypes() throws Exception { | ||
assertResult("17", "VarSample", "VarSampleType1.pass", 1063); | ||
assertResult("17", "VarSample", "VarSampleType2.pass", 1088); | ||
assertResult("17", "VarSample", "VarSampleType3.pass", 1172); | ||
} | ||
|
||
private void assertResult(String sourceLevel, String source, String goldenFileName, int pos) throws Exception { | ||
this.sourceLevel.set(sourceLevel); | ||
File testSource = new File(getWorkDir(), "test/Test.java"); | ||
testSource.getParentFile().mkdirs(); | ||
copyToWorkDir(new File(getDataDir(), "org/netbeans/modules/java/completion/data/" + source + ".java"), testSource); | ||
FileObject testSourceFO = FileUtil.toFileObject(testSource); | ||
assertNotNull(testSourceFO); | ||
DataObject testSourceDO = DataObject.find(testSourceFO); | ||
assertNotNull(testSourceDO); | ||
EditorCookie ec = (EditorCookie) testSourceDO.getCookie(EditorCookie.class); | ||
assertNotNull(ec); | ||
final Document doc = ec.openDocument(); | ||
assertNotNull(doc); | ||
doc.putProperty(Language.class, JavaTokenId.language()); | ||
doc.putProperty("mimeType", "text/x-java"); | ||
Source s = Source.create(doc); | ||
JavaTooltipTask task = JavaTooltipTask.create(pos, () -> false); | ||
ParserManager.parse(Collections.singletonList(s), task); | ||
|
||
String reference = getGoldFileContents(goldenFileName); | ||
StringWriter result = new StringWriter(); | ||
result.write("Query offset: "); | ||
result.write(String.valueOf(pos)); | ||
result.write("\n"); | ||
result.write("Anchor offset: "); | ||
result.write(String.valueOf(task.getAnchorOffset())); | ||
result.write("\n"); | ||
result.write("Tooltip offset: "); | ||
result.write(String.valueOf(task.getTooltipOffset())); | ||
result.write("\n"); | ||
result.write("Tooltip index: "); | ||
result.write(String.valueOf(task.getTooltipIndex())); | ||
result.write("\n\n"); | ||
|
||
if (task.getTooltipData() != null) { | ||
result.write("[\n"); | ||
for (List<String> element : task.getTooltipData()) { | ||
result.write("\t[\n"); | ||
for (String type : element) { | ||
result.write("\t\t"); | ||
result.write(type); | ||
result.write("\n"); | ||
} | ||
result.write("\t[\n"); | ||
} | ||
result.write("]\n"); | ||
} | ||
|
||
assertEquals(reference, result.toString()); | ||
} | ||
|
||
@SuppressWarnings("NestedAssignment") | ||
private String getGoldFileContents(String goldenFileName) throws IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
File goldenFile = getGoldenFile(goldenFileName); | ||
try(InputStream is = new FileInputStream(goldenFile)) { | ||
int read; | ||
byte[] buffer = new byte[1024]; | ||
while((read = is.read(buffer)) > 0) { | ||
baos.write(buffer, 0, read); | ||
} | ||
} | ||
return baos.toString("UTF-8"); | ||
} | ||
} |