Skip to content

Commit

Permalink
jarek/7289: autocomplete for classpath magic command (#7820)
Browse files Browse the repository at this point in the history
* #7289: add autocomplete for classpath magic command

* #7289: ignore invalidPathException
  • Loading branch information
jaroslawmalekcodete authored and LeeTZ committed Oct 12, 2018
1 parent a042463 commit adacca9
Show file tree
Hide file tree
Showing 37 changed files with 817 additions and 83 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jupyter_contrib_nbextensions/
/gradle*
kernel/build/
kernel/*/build/
kernel/*/out/
/build/
/.nb-gradle/
.nb-gradle-properties
Expand Down
13 changes: 13 additions & 0 deletions doc/groovy/ClasspathMagicCommands.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,19 @@
"name": "Groovy",
"nbconverter_exporter": "",
"version": "2.4.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": false,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": false,
"toc_window_display": false
}
},
"nbformat": 4,
Expand Down
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);
}
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;
};
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

import java.util.List;

import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;
import static org.apache.commons.lang3.builder.HashCodeBuilder.reflectionHashCode;
import static org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString;

public class AutocompleteResult {

private List<String> matches;
Expand All @@ -25,6 +29,7 @@ public class AutocompleteResult {
public AutocompleteResult(List<String> matches, int startIndex) {
this.matches = matches;
this.startIndex = startIndex;

}

public List<String> getMatches() {
Expand All @@ -35,5 +40,19 @@ public int getStartIndex() {
return startIndex;
}

@Override
public boolean equals(Object o) {
return reflectionEquals(this, o);
}

@Override
public int hashCode() {
return reflectionHashCode(this);
}

@Override
public String toString() {
return reflectionToString(this);
}

}
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);

}
Loading

0 comments on commit adacca9

Please sign in to comment.