package org.javacs;

import com.sun.tools.javac.code.Symbol;
import io.typefox.lsapi.*;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

public class SymbolIndexTest {
    private static final Logger LOG = Logger.getLogger("main");

    @Test
    public void all() {
        Set<String> all = search("");

        assertThat(all, not(empty()));
    }

    @Test
    public void searchClasses() {
        Set<String> all = search("JLanguageServer");

        assertThat(all, hasItem("JavaLanguageServer"));
    }

    @Test
    public void searchMethods() {
        Set<String> all = search("gTextDocumentService");

        assertThat(all, hasItem("getTextDocumentService"));
    }

    private Set<String> search(String query) {
        return index().search(query).map(s -> s.getName()).collect(Collectors.toSet());
    }

    private static SymbolIndex index() {
        if (index == null)
            index = getIndex();

        return index;
    }

    private static SymbolIndex index;
    
    private static SymbolIndex getIndex() {
        Set<Path> classPath = Collections.emptySet();
        Set<Path> sourcePath = Collections.singleton(Paths.get("src/main/java").toAbsolutePath());
        Path outputDirectory = Paths.get("out").toAbsolutePath();
        SymbolIndex index = new SymbolIndex(classPath, sourcePath, outputDirectory, (paths, errs) -> {
            errs.getDiagnostics().forEach(d -> LOG.info(d.getMessage(Locale.US)));
        });

        index.initialIndexComplete.join();

        return index;
    }
}
@@ -236,20 +236,21 @@
 io.file.
-Path
+Files
 ;%0Aimport
@@ -268,17 +268,16 @@
 ile.Path
-s
 ;%0Aimport
@@ -286,72 +286,22 @@
 ava.
-util.Collections;%0Aimport java.util.List;%0Aimport java.util.Locale
+nio.file.Paths
 ;%0Aim
@@ -315,19 +315,17 @@
 va.util.
-Set
+*
 ;%0Aimport
@@ -1400,32 +1400,50 @@
 ex getIndex() %7B%0A
+        try %7B%0A    
         Set%3CPath
@@ -1460,32 +1460,268 @@
 h = 
-Collections.emptySet
+new HashSet%3C%3E();%0A%0A            for (String line : Files.readAllLines(Paths.get(%22classpath.txt%22))) %7B%0A                for (String entry : line.split(%22:%22)) %7B%0A                    classPath.add(Paths.get(entry).toAbsolutePath
 ()
+)
 ;%0A
+                %7D%0A            %7D%0A    
     
@@ -1823,16 +1823,20 @@
         
+    
 Path out
@@ -1893,16 +1893,20 @@
         
+   
+ 
 SymbolIn
@@ -2004,16 +2004,20 @@
         
+    
 errs.get
@@ -2087,16 +2087,20 @@
         
+    
 %7D);%0A%0A   
@@ -2100,24 +2100,28 @@
 );%0A%0A        
+  
+  
 index.initia
@@ -2144,32 +2144,36 @@
 oin();%0A%0A        
+    
 return index;%0A  
@@ -2166,19 +2166,106 @@
 turn index;%0A
+        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D%0A
     %7D%0A%7D

Fix classpath