ProjectConfigRegistry implements ProjectComponent, Disposable { @Nullable public ClientConfigRoot getClientFor(@Nullable VirtualFile file) { if (file == null) { return null; } int closestDepth = Integer.MAX_VALUE; ClientConfigRoot closest = null; for (ClientConfigRoot clientConfigRoot : getRegisteredStates()) { int depth = FileTreeUtil.getPathDepth(file, clientConfigRoot.getClientRootDir()); if (depth >= 0 && depth < closestDepth) { closestDepth = depth; closest = clientConfigRoot; } } return closest; } protected  ProjectConfigRegistry(@NotNull Project project); @Nullable static ProjectConfigRegistry getInstance(@NotNull Project project); @Nullable ClientConfigRoot getClientFor(@Nullable VirtualFile file); @Nullable ClientConfigRoot getClientFor(@Nullable FilePath file); boolean isOnline(@Nullable ClientServerRef clientServerRef); @NotNull Collection<ClientConfigRoot> getClientConfigRoots(); @Nullable abstract ClientConfig getRegisteredClientConfigState(@NotNull ClientServerRef ref); abstract void addClientConfig(@NotNull ClientConfig config, @NotNull VirtualFile vcsRootDir); abstract boolean removeClientConfigAt(@NotNull VirtualFile vcsRootDir); @Override void projectOpened(); @Override final void projectClosed(); @Override void initComponent(); boolean isDisposed(); @Override void dispose(); @Override final void disposeComponent(); @NotNull @Override final String getComponentName(); static final Class<ProjectConfigRegistry> COMPONENT_CLASS; static final String COMPONENT_NAME; }
@Test void getClientFor() { TestableProjectConfigRegistry registry = new TestableProjectConfigRegistry(idea.getMockProject()); Map<String, MockVirtualFile> vfs = MockVirtualFileSystem.createTree( "one/two/three/four/five.txt", "contents", "root.txt", "contents", "one/root.txt", "contents", "one/two/root.txt", "contents" ); MockVirtualFile five = vfs.get("one/two/three/four/five.txt"); MockVirtualFile two = vfs.get("one/two/root.txt"); MockVirtualFile one = vfs.get("one/root.txt"); MockVirtualFile zero = vfs.get("root.txt"); MockConfigPart cp1 = new MockConfigPart() .withClientname("cn") .withUsername("u") .withServerName("s:123"); ClientConfig cc1 = ClientConfig.createFrom(ServerConfig.createFrom(cp1), cp1); registry.addClientConfig(cc1, one.getParent()); MockConfigPart cp2 = new MockConfigPart() .withClientname("cn0") .withUsername("u0") .withServerName("s:123"); ClientConfig cc2 = ClientConfig.createFrom(ServerConfig.createFrom(cp2), cp2); registry.addClientConfig(cc2, two.getParent()); assertNotNull(registry.getClientFor(five)); assertSame(cc2, registry.getClientFor(five).getClientConfig()); assertNotNull(registry.getClientFor(five.asFilePath())); assertSame(cc2, registry.getClientFor(five.asFilePath()).getClientConfig()); assertNotNull(registry.getClientFor(one.asFilePath())); assertSame(cc1, registry.getClientFor(one.asFilePath()).getClientConfig()); assertNull(registry.getClientFor(zero)); assertNull(registry.getClientFor(zero.asFilePath())); }