Code completion for the ids in the XHTML page
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 29 Oct 2012 13:05:56 +0100
changeset 11702618d8bec44
parent 116 033d51e026b0
child 122 0a582b5a2737
Code completion for the ids in the XHTML page
htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
     1.1 --- a/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Tue Oct 23 16:50:27 2012 +0200
     1.2 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Mon Oct 29 13:05:56 2012 +0100
     1.3 @@ -21,12 +21,18 @@
     1.4  import java.io.InputStream;
     1.5  import java.io.OutputStreamWriter;
     1.6  import java.io.Writer;
     1.7 +import java.util.ArrayList;
     1.8 +import java.util.Collections;
     1.9 +import java.util.List;
    1.10  import java.util.Locale;
    1.11  import java.util.Set;
    1.12  import javax.annotation.processing.AbstractProcessor;
    1.13 +import javax.annotation.processing.Completion;
    1.14 +import javax.annotation.processing.Completions;
    1.15  import javax.annotation.processing.Processor;
    1.16  import javax.annotation.processing.RoundEnvironment;
    1.17  import javax.annotation.processing.SupportedAnnotationTypes;
    1.18 +import javax.lang.model.element.AnnotationMirror;
    1.19  import javax.lang.model.element.Element;
    1.20  import javax.lang.model.element.ElementKind;
    1.21  import javax.lang.model.element.ExecutableElement;
    1.22 @@ -167,4 +173,47 @@
    1.23          }
    1.24          return true;
    1.25      }
    1.26 +
    1.27 +    @Override
    1.28 +    public Iterable<? extends Completion> getCompletions(
    1.29 +        Element element, AnnotationMirror annotation, 
    1.30 +        ExecutableElement member, String userText
    1.31 +    ) {
    1.32 +        if (!userText.startsWith("\"")) {
    1.33 +            return Collections.emptyList();
    1.34 +        }
    1.35 +        
    1.36 +        Element cls = findClass(element);
    1.37 +        Page p = cls.getAnnotation(Page.class);
    1.38 +        PackageElement pe = (PackageElement) cls.getEnclosingElement();
    1.39 +        String pkg = pe.getQualifiedName().toString();
    1.40 +        ProcessPage pp;
    1.41 +        try {
    1.42 +            InputStream is = openStream(pkg, p.xhtml());
    1.43 +            pp = ProcessPage.readPage(is);
    1.44 +            is.close();
    1.45 +        } catch (IOException iOException) {
    1.46 +            return Collections.emptyList();
    1.47 +        }
    1.48 +        
    1.49 +        List<Completion> cc = new ArrayList<Completion>();
    1.50 +        userText = userText.substring(1);
    1.51 +        for (String id : pp.ids()) {
    1.52 +            if (id.startsWith(userText)) {
    1.53 +                cc.add(Completions.of("\"" + id + "\"", id));
    1.54 +            }
    1.55 +        }
    1.56 +        return cc;
    1.57 +    }
    1.58 +    
    1.59 +    private static Element findClass(Element e) {
    1.60 +        if (e == null) {
    1.61 +            return null;
    1.62 +        }
    1.63 +        Page p = e.getAnnotation(Page.class);
    1.64 +        if (p != null) {
    1.65 +            return e;
    1.66 +        }
    1.67 +        return e.getEnclosingElement();
    1.68 +    }
    1.69  }