Skip to content

Commit adacca9

Browse files
jaroslawmalekcodeteLeeTZ
authored andcommitted
jarek/7289: autocomplete for classpath magic command (#7820)
* #7289: add autocomplete for classpath magic command * #7289: ignore invalidPathException
1 parent a042463 commit adacca9

File tree

37 files changed

+817
-83
lines changed

37 files changed

+817
-83
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jupyter_contrib_nbextensions/
2323
/gradle*
2424
kernel/build/
2525
kernel/*/build/
26+
kernel/*/out/
2627
/build/
2728
/.nb-gradle/
2829
.nb-gradle-properties

doc/groovy/ClasspathMagicCommands.ipynb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,19 @@
423423
"name": "Groovy",
424424
"nbconverter_exporter": "",
425425
"version": "2.4.3"
426+
},
427+
"toc": {
428+
"base_numbering": 1,
429+
"nav_menu": {},
430+
"number_sections": false,
431+
"sideBar": false,
432+
"skip_h1_title": false,
433+
"title_cell": "Table of Contents",
434+
"title_sidebar": "Contents",
435+
"toc_cell": false,
436+
"toc_position": {},
437+
"toc_section_display": false,
438+
"toc_window_display": false
426439
}
427440
},
428441
"nbformat": 4,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.twosigma.beakerx;
17+
18+
import java.util.Arrays;
19+
import java.util.Collection;
20+
import java.util.LinkedList;
21+
import java.util.List;
22+
23+
public abstract class AutocompleteNode {
24+
25+
public static final List<AutocompleteNode> NO_CHILDREN = Arrays.asList();
26+
private String name;
27+
private List<AutocompleteNode> children;
28+
29+
public AutocompleteNode(String name, List<AutocompleteNode> children) {
30+
this.name = name;
31+
this.children = children;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public Collection<AutocompleteNode> getChildren() {
39+
return children;
40+
}
41+
42+
public abstract List<String> matchToTheWord(LinkedList<String> parts, String last);
43+
44+
public abstract List<String> findNextWord(LinkedList<String> parts);
45+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.twosigma.beakerx;
17+
18+
import org.jetbrains.annotations.NotNull;
19+
20+
import java.io.IOException;
21+
import java.nio.file.DirectoryStream;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.ArrayList;
26+
import java.util.LinkedList;
27+
import java.util.List;
28+
29+
public class AutocompleteNodeFileSystem extends AutocompleteNode {
30+
31+
public AutocompleteNodeFileSystem(String name, List<AutocompleteNode> children) {
32+
super(name, children);
33+
}
34+
35+
@Override
36+
public List<String> matchToTheWord(LinkedList<String> parts, String last) {
37+
return list(parts, last);
38+
}
39+
40+
@Override
41+
public List<String> findNextWord(LinkedList<String> parts) {
42+
String dir = ".";
43+
return list(parts, dir);
44+
}
45+
46+
@NotNull
47+
private List<String> list(LinkedList<String> parts, String dir) {
48+
if (parts.isEmpty()) {
49+
try {
50+
Path path = Paths.get(dir);
51+
if (Files.exists(path)) {
52+
return listDirectory(path);
53+
} else {
54+
return listParentDirectory(path);
55+
}
56+
} catch (Exception e) {
57+
return new ArrayList<>();
58+
}
59+
}
60+
return new ArrayList<>();
61+
}
62+
63+
@NotNull
64+
private List<String> listParentDirectory(Path path) throws IOException {
65+
List<String> result = new ArrayList<>();
66+
Path parent = path.getParent();
67+
Files.newDirectoryStream(parent, p -> {
68+
if (p.toString().startsWith(path.toString())) {
69+
if (p.toFile().isFile()) {
70+
return p.toString().endsWith(".jar");
71+
}
72+
return true;
73+
}
74+
return false;
75+
}).forEach(x -> result.add(x.toString()));
76+
return result;
77+
}
78+
79+
@NotNull
80+
private List<String> listDirectory(Path path) throws IOException {
81+
List<String> result = new ArrayList<>();
82+
Files.newDirectoryStream(path, filter)
83+
.forEach(x -> result.add(x.toString()));
84+
return result;
85+
}
86+
87+
private DirectoryStream.Filter<Path> filter = path -> {
88+
if (path.toFile().isFile()) {
89+
return path.toString().endsWith(".jar");
90+
}
91+
return true;
92+
};
93+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.twosigma.beakerx;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collection;
20+
import java.util.LinkedList;
21+
import java.util.List;
22+
import java.util.Optional;
23+
import java.util.stream.Collectors;
24+
25+
public class AutocompleteNodeStatic extends AutocompleteNode {
26+
27+
public AutocompleteNodeStatic(String name, List<AutocompleteNode> children) {
28+
super(name, children);
29+
}
30+
31+
public List<String> matchToTheWord(LinkedList<String> parts, String last) {
32+
if (parts.isEmpty()) {
33+
return findMatches(getChildren(), last);
34+
} else {
35+
Optional<AutocompleteNode> node = findNode(parts);
36+
if (node.isPresent()) {
37+
return node.get().matchToTheWord(parts, last);
38+
}
39+
return new ArrayList<>();
40+
}
41+
}
42+
43+
public List<String> findNextWord(LinkedList<String> parts) {
44+
if (parts.isEmpty()) {
45+
return getChildren().stream().map(x->x.getName()).collect(Collectors.toList());
46+
} else {
47+
Optional<AutocompleteNode> node = findNode(parts);
48+
if (node.isPresent()) {
49+
return node.get().findNextWord(parts);
50+
}
51+
return new ArrayList<>();
52+
}
53+
}
54+
55+
private List<String> findMatches(Collection<AutocompleteNode> nodes, String txt) {
56+
return nodes.stream()
57+
.filter(x -> x.getName().startsWith(txt))
58+
.filter(x -> !x.getName().equals(txt))
59+
.map(x -> x.getName())
60+
.collect(Collectors.toList());
61+
}
62+
63+
64+
65+
private Optional<AutocompleteNode> findNode(LinkedList<String> parts) {
66+
String first = parts.removeFirst();
67+
return getChildren().stream().filter(x -> x.getName().equals(first)).findFirst();
68+
}
69+
}

kernel/base/src/main/java/com/twosigma/beakerx/autocomplete/AutocompleteResult.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
import java.util.List;
1919

20+
import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;
21+
import static org.apache.commons.lang3.builder.HashCodeBuilder.reflectionHashCode;
22+
import static org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString;
23+
2024
public class AutocompleteResult {
2125

2226
private List<String> matches;
@@ -25,6 +29,7 @@ public class AutocompleteResult {
2529
public AutocompleteResult(List<String> matches, int startIndex) {
2630
this.matches = matches;
2731
this.startIndex = startIndex;
32+
2833
}
2934

3035
public List<String> getMatches() {
@@ -35,5 +40,19 @@ public int getStartIndex() {
3540
return startIndex;
3641
}
3742

43+
@Override
44+
public boolean equals(Object o) {
45+
return reflectionEquals(this, o);
46+
}
47+
48+
@Override
49+
public int hashCode() {
50+
return reflectionHashCode(this);
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return reflectionToString(this);
56+
}
3857

3958
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.twosigma.beakerx.evaluator;
17+
18+
import com.twosigma.beakerx.autocomplete.AutocompleteResult;
19+
20+
public interface AutocompleteService {
21+
22+
AutocompleteResult find(String txt, int cur);
23+
24+
}

0 commit comments

Comments
 (0)