提交 5e999233 编写于 作者: K Kohsuke Kawaguchi

adding auto-completion method

上级 18561d5f
......@@ -25,6 +25,7 @@
package hudson.model;
import hudson.search.Search;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
......@@ -71,4 +72,71 @@ public class AutoCompletionCandidates implements HttpResponse {
}
rsp.serveExposedBean(req,r, Flavor.JSON);
}
/**
* Auto-completes possible job names.
*
* @param type
* Limit the auto-completion to the subtype of this type.
* @param value
* The value the user has typed in. Matched as a prefix.
* @param self
* The contextual item for which the auto-completion is provided to.
* For example, if you are configuring a job, this is the job being configured.
* @param container
* The nearby contextual {@link ItemGroup} to resolve relative job names from.
*/
public static <T extends Item> AutoCompletionCandidates ofJobNames(final Class<T> type, final String value, Item self, ItemGroup container) {
if (self==container)
container = self.getParent();
final AutoCompletionCandidates candidates = new AutoCompletionCandidates();
class Visitor extends ItemVisitor {
String prefix;
Visitor(String prefix) {
this.prefix = prefix;
}
@Override
public void onItem(Item i) {
String n = contextualNameOf(i);
if ((n.startsWith(value) || value.startsWith(n))
// 'foobar' is a valid candidate if the current value is 'foo'.
// Also, we need to visit 'foo' if the current value is 'foo/bar'
&& (value.length()>n.length() || !n.substring(value.length()).contains("/"))
// but 'foobar/zot' isn't if the current value is 'foo'
// we'll first show 'foobar' and then wait for the user to type '/' to show the rest
&& i.hasPermission(Item.READ)
// and read permission required
) {
if (type.isInstance(i) && n.startsWith(value))
candidates.add(n);
// recurse
String oldPrefix = prefix;
prefix = n;
super.onItem(i);
prefix = oldPrefix;
}
}
private String contextualNameOf(Item i) {
if (prefix.endsWith("/") || prefix.length()==0)
return prefix+i.getName();
else
return prefix+'/'+i.getName();
}
}
if (container==null || container==Jenkins.getInstance()) {
new Visitor("").onItemGroup(Jenkins.getInstance());
} else {
new Visitor("").onItemGroup(container);
if (value.startsWith("/"))
new Visitor("/").onItemGroup(Jenkins.getInstance());
}
return candidates;
}
}
package hudson.model;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixProject;
import hudson.matrix.TextAxis;
import org.jvnet.hudson.test.HudsonTestCase;
import java.util.Arrays;
import java.util.TreeSet;
/**
* @author Kohsuke Kawaguchi
*/
public class AutoCompletionCandidatesTest extends HudsonTestCase {
public void testCompletion() throws Exception {
FreeStyleProject foo = jenkins.createProject(FreeStyleProject.class, "foo");
MatrixProject bar = jenkins.createProject(MatrixProject.class, "bar");
bar.setAxes(new AxisList(new TextAxis("x","1","2","3")));
MatrixConfiguration x3 = bar.getItem("x=3");
AutoCompletionCandidates c;
c = AutoCompletionCandidates.ofJobNames(Item.class, "", foo, jenkins);
assertContains(c, "foo", "bar");
c = AutoCompletionCandidates.ofJobNames(Item.class, "ba", foo, jenkins);
assertContains(c, "bar");
c = AutoCompletionCandidates.ofJobNames(Item.class, "bar/", foo, jenkins);
assertContains(c, "bar/x=1", "bar/x=2", "bar/x=3");
c = AutoCompletionCandidates.ofJobNames(FreeStyleProject.class, "", foo, jenkins);
assertContains(c, "foo");
c = AutoCompletionCandidates.ofJobNames(MatrixConfiguration.class, "bar/", foo, jenkins);
assertContains(c, "bar/x=1", "bar/x=2", "bar/x=3");
c = AutoCompletionCandidates.ofJobNames(Item.class, "", x3, x3.getParent());
assertContains(c, "x=1", "x=2", "x=3");
c = AutoCompletionCandidates.ofJobNames(Item.class, "/", x3, x3.getParent());
assertContains(c, "/foo", "/bar");
c = AutoCompletionCandidates.ofJobNames(Item.class, "/bar/", x3, x3.getParent());
assertContains(c, "/bar/x=1", "/bar/x=2", "/bar/x=3");
}
private void assertContains(AutoCompletionCandidates c, String... values) {
assertEquals(new TreeSet<String>(Arrays.asList(values)), new TreeSet<String>(c.getValues()));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册