-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jarek/7289: autocomplete for classpath magic command (#7820)
- Loading branch information
1 parent
a042463
commit adacca9
Showing
37 changed files
with
817 additions
and
83 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
45 changes: 45 additions & 0 deletions
45
kernel/base/src/main/java/com/twosigma/beakerx/AutocompleteNode.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,45 @@ | ||
/* | ||
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed 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 com.twosigma.beakerx; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public abstract class AutocompleteNode { | ||
|
||
public static final List<AutocompleteNode> NO_CHILDREN = Arrays.asList(); | ||
private String name; | ||
private List<AutocompleteNode> children; | ||
|
||
public AutocompleteNode(String name, List<AutocompleteNode> children) { | ||
this.name = name; | ||
this.children = children; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public Collection<AutocompleteNode> getChildren() { | ||
return children; | ||
} | ||
|
||
public abstract List<String> matchToTheWord(LinkedList<String> parts, String last); | ||
|
||
public abstract List<String> findNextWord(LinkedList<String> parts); | ||
} |
93 changes: 93 additions & 0 deletions
93
kernel/base/src/main/java/com/twosigma/beakerx/AutocompleteNodeFileSystem.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,93 @@ | ||
/* | ||
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed 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 com.twosigma.beakerx; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.DirectoryStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class AutocompleteNodeFileSystem extends AutocompleteNode { | ||
|
||
public AutocompleteNodeFileSystem(String name, List<AutocompleteNode> children) { | ||
super(name, children); | ||
} | ||
|
||
@Override | ||
public List<String> matchToTheWord(LinkedList<String> parts, String last) { | ||
return list(parts, last); | ||
} | ||
|
||
@Override | ||
public List<String> findNextWord(LinkedList<String> parts) { | ||
String dir = "."; | ||
return list(parts, dir); | ||
} | ||
|
||
@NotNull | ||
private List<String> list(LinkedList<String> parts, String dir) { | ||
if (parts.isEmpty()) { | ||
try { | ||
Path path = Paths.get(dir); | ||
if (Files.exists(path)) { | ||
return listDirectory(path); | ||
} else { | ||
return listParentDirectory(path); | ||
} | ||
} catch (Exception e) { | ||
return new ArrayList<>(); | ||
} | ||
} | ||
return new ArrayList<>(); | ||
} | ||
|
||
@NotNull | ||
private List<String> listParentDirectory(Path path) throws IOException { | ||
List<String> result = new ArrayList<>(); | ||
Path parent = path.getParent(); | ||
Files.newDirectoryStream(parent, p -> { | ||
if (p.toString().startsWith(path.toString())) { | ||
if (p.toFile().isFile()) { | ||
return p.toString().endsWith(".jar"); | ||
} | ||
return true; | ||
} | ||
return false; | ||
}).forEach(x -> result.add(x.toString())); | ||
return result; | ||
} | ||
|
||
@NotNull | ||
private List<String> listDirectory(Path path) throws IOException { | ||
List<String> result = new ArrayList<>(); | ||
Files.newDirectoryStream(path, filter) | ||
.forEach(x -> result.add(x.toString())); | ||
return result; | ||
} | ||
|
||
private DirectoryStream.Filter<Path> filter = path -> { | ||
if (path.toFile().isFile()) { | ||
return path.toString().endsWith(".jar"); | ||
} | ||
return true; | ||
}; | ||
} |
69 changes: 69 additions & 0 deletions
69
kernel/base/src/main/java/com/twosigma/beakerx/AutocompleteNodeStatic.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,69 @@ | ||
/* | ||
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed 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 com.twosigma.beakerx; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
public class AutocompleteNodeStatic extends AutocompleteNode { | ||
|
||
public AutocompleteNodeStatic(String name, List<AutocompleteNode> children) { | ||
super(name, children); | ||
} | ||
|
||
public List<String> matchToTheWord(LinkedList<String> parts, String last) { | ||
if (parts.isEmpty()) { | ||
return findMatches(getChildren(), last); | ||
} else { | ||
Optional<AutocompleteNode> node = findNode(parts); | ||
if (node.isPresent()) { | ||
return node.get().matchToTheWord(parts, last); | ||
} | ||
return new ArrayList<>(); | ||
} | ||
} | ||
|
||
public List<String> findNextWord(LinkedList<String> parts) { | ||
if (parts.isEmpty()) { | ||
return getChildren().stream().map(x->x.getName()).collect(Collectors.toList()); | ||
} else { | ||
Optional<AutocompleteNode> node = findNode(parts); | ||
if (node.isPresent()) { | ||
return node.get().findNextWord(parts); | ||
} | ||
return new ArrayList<>(); | ||
} | ||
} | ||
|
||
private List<String> findMatches(Collection<AutocompleteNode> nodes, String txt) { | ||
return nodes.stream() | ||
.filter(x -> x.getName().startsWith(txt)) | ||
.filter(x -> !x.getName().equals(txt)) | ||
.map(x -> x.getName()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
|
||
|
||
private Optional<AutocompleteNode> findNode(LinkedList<String> parts) { | ||
String first = parts.removeFirst(); | ||
return getChildren().stream().filter(x -> x.getName().equals(first)).findFirst(); | ||
} | ||
} |
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
24 changes: 24 additions & 0 deletions
24
kernel/base/src/main/java/com/twosigma/beakerx/evaluator/AutocompleteService.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,24 @@ | ||
/* | ||
* Copyright 2018 TWO SIGMA OPEN SOURCE, LLC | ||
* | ||
* Licensed 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 com.twosigma.beakerx.evaluator; | ||
|
||
import com.twosigma.beakerx.autocomplete.AutocompleteResult; | ||
|
||
public interface AutocompleteService { | ||
|
||
AutocompleteResult find(String txt, int cur); | ||
|
||
} |
Oops, something went wrong.