BBx-Kitchen / bbj-language-server

BBj Language Server
MIT License
6 stars 6 forks source link

keyword help - link to BASIS documentation #113

Closed StephanWald closed 1 week ago

StephanWald commented 11 months ago

Eclipse BDT had an additional "Keyword Help" plug-in that linked directly to documentation. This is desirable from VSCode, too.

StephanWald commented 11 months ago
package com.basis.bbjutilities.bbjdocsgenerator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Indexer
{
    private static final String INDEX_URL = "https://documentation.basis.cloud/BASISHelp/WebHelp/contents/BASISHelp-Index.htm";
    private static final String LINK_PREFIX = "https://documentation.basis.cloud/BASISHelp/WebHelp";

    private static HashMap<String, String> index;

    public Indexer()
    {
        initializeIndex();
    }

    private void initializeIndex()
    {
        try
        {
            buildIndex();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    private void buildIndex() throws IOException
    {
        URL url = new URL(INDEX_URL);

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(url.openStream()));

        String line = "";
        StringBuilder builder = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            builder.append(line);
        }

        reader.close();

        String anchor = "<h1>BASISHelp Index</h1>";

        String html = builder.toString();
        if (html.contains(anchor))
        {
            html = html.substring(html.indexOf(anchor) + anchor.length());
        }

        Pattern pattern = Pattern.compile(
                "<a href=\\\"\\.\\.(.*?)\\\">(.*?)<\\/a>", Pattern.DOTALL);
        Matcher matcher = pattern.matcher(html);

        index = new HashMap<>();

        while (matcher.find())
        {
            if (!index.containsKey(matcher.group(2).trim()))
            {
                index.put(matcher.group(2).trim(), matcher.group(1));
            }
        }
    }

    public ArrayList<String> getMatchingTopics(String word)
    {

        ArrayList<String> topicList = new ArrayList<>();

        // invalid word state check
        if (word == null || word.length() <= 2)
        {
            return topicList;
        }

        String searchKeyword = word.toUpperCase();

        String topic;
        Iterator<String> it = index.keySet().iterator();

        while (it.hasNext())
        {
            topic = it.next();

            if (!topic.toUpperCase().contains(searchKeyword))
            {
                continue;
            }

            if (!topic.contains("::"))
            {
                topicList.add(topic);
            }
        }

        return topicList;

    }

    /**
     * Returns the index
     * 
     * @return HashMap<String,String>
     */
    public HashMap<String, String> getIndex()
    {
        if (index != null)
        {
            return index;
        }

        return new HashMap<>();
    }

    /**
     * Returns the documentation url for the given topic name.
     * 
     * @param string
     * @return
     */
    public String getUrl(String string)
    {
        if (index.containsKey(string))
        {
            return LINK_PREFIX + index.get(string);
        }

        return "";
    }
}
StephanWald commented 1 week ago

Javadoc is being supported