dbalek commented on code in PR #4040:
URL: https://github.com/apache/netbeans/pull/4040#discussion_r923264018
##########
java/java.lsp.server/src/org/netbeans/modules/java/lsp/server/htmlui/Browser.java:
##########
@@ -18,538 +18,168 @@
*/
package org.netbeans.modules.java.lsp.server.htmlui;
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
import java.io.Closeable;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.Flushable;
import java.io.IOException;
-import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.OutputStream;
import java.io.Reader;
-import java.io.Writer;
-import java.net.URI;
-import java.net.URISyntaxException;
import java.net.URL;
-import java.net.URLConnection;
import java.net.URLDecoder;
-import java.nio.file.Files;
-import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
-import java.util.Queue;
import java.util.Random;
+import java.util.Set;
import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
import java.util.function.Consumer;
-import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
+import org.eclipse.lsp4j.CodeAction;
+import org.eclipse.lsp4j.CodeActionParams;
import org.netbeans.html.boot.spi.Fn;
-import org.netbeans.html.boot.spi.Fn.Presenter;
import org.netbeans.html.presenters.spi.ProtoPresenter;
import org.netbeans.html.presenters.spi.ProtoPresenterBuilder;
+import org.netbeans.modules.java.lsp.server.protocol.CodeActionsProvider;
+import org.netbeans.modules.java.lsp.server.protocol.HtmlPageParams;
+import org.netbeans.modules.java.lsp.server.protocol.NbCodeLanguageClient;
+import org.netbeans.modules.parsing.api.ResultIterator;
+import org.netbeans.spi.htmlui.HTMLViewerSpi;
+import org.openide.util.Exceptions;
+import org.openide.util.lookup.ServiceProvider;
-/** Browser based {@link Presenter}. It starts local server and
- * launches browser that connects to it. Use {@link Browser.Config} to
- * configure the actual browser to be started.
- * <p>
- * To use this presenter specify following dependency:
- * <pre>
- * <dependency>
- * <groupId>org.netbeans.html.browser</groupId>
- * <artifactId>browser</artifactId>
- * <version><a target="blank"
href="http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.dukescript.presenters%22%20AND%20a%3A%22browser%22">1.x</a></version>
- * </dependency>
- * </pre>
- */
-public final class Browser implements Fn.Presenter, Fn.KeepAlive, Flushable,
-Executor, Closeable {
- static final Logger LOG = Logger.getLogger(Browser.class.getName());
- private final Map<String,Command> SESSIONS = new HashMap<>();
+public class Browser implements Closeable {
+
+ private static final Logger LOG =
Logger.getLogger(Browser.class.getName());
+
+ private final Config config;
private final String app;
- private HttpServer server;
private Runnable onPageLoad;
- private Command current;
- private final Config config;
- private final Supplier<HttpServer<?, ?, ?, ?>> serverProvider;
-
- /** Default constructor. Reads configuration from properties. The actual
browser to
- * be launched can be influenced by value of
- * <code>com.dukescript.presenters.browser</code> property.
- * It can have following values:
- * <ul>
- * <li><b>GTK</b> - use Gtk WebKit implementation. Requires presence of
- * appropriate native libraries</li>
- * <li><b>AWT</b> - use {@link java.awt.Desktop#browse(java.net.URI)} to
- * launch a browser</li>
- * <li><b>NONE</b> - just launches the server, useful together with
- * <code>com.dukescript.presenters.browserPort</code> property that
- * can specify a fixed port to open the server at
- * </li>
- * <li>any other value is interpreted as a command which is then
- * launched on a command line with one parameter - the URL to connect
to</li>
- * </ul>
- * If the property is not specified the system tries <b>GTK</b> mode first,
- * followed by <b>AWT</b> and then tries to execute <code>xdg-open</code>
- * (default LINUX command to launch a browser from a shell script).
- * <p>
- * In addition to the above properties, it is possible to also enable
- * debugging by setting
<code>com.dukescript.presenters.browserDebug=true</code>.
- *
- * @throws Exception
- */
- public Browser() {
- this(new Config());
- }
+ private String id;
- /**
- * Browser configured by provided config.
- *
- * @param config the configuration
- */
public Browser(Config config) {
- this(findCalleeClassName(), config, null);
- }
-
- Browser(String app, Config config, Supplier<HttpServer<?,?,?, ?>>
serverProvider) {
- this.serverProvider = serverProvider != null ? serverProvider : () -> {
- return new SimpleServer(config.random);
- };
- this.app = app;
- this.config = new Config(config);
- }
-
- @Override
- public final void execute(final Runnable r) {
- current.execute(r);
- }
-
- @Override
- public void close() throws IOException {
- if (server != null) {
- server.shutdownNow();
- }
- }
-
- HttpServer server() {
- return server;
- }
-
- static HttpServer findServer(Object obj) {
- Command c;
- if (obj instanceof Command) {
- c = (Command) obj;
- } else if (obj instanceof ProtoPresenter) {
- c = ((ProtoPresenter) obj).lookup(Command.class);
- } else {
- throw new IllegalArgumentException("Cannot find server for " +
obj);
- }
- return c.browser.server();
- }
-
- /** Shows URL in a browser.
- * @param page the page to display in the browser
- * @throws IOException if something goes wrong
- */
- void show(URI page) throws IOException {
- config.getBrowser().accept(page);
- }
- @Override
- public Fn defineFn(String string, String... strings) {
- throw new UnsupportedOperationException();
+ this.config = config;
+ this.app = findCalleeClassName();
}
- @Override
- public void loadScript(Reader reader) throws Exception {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public Fn defineFn(String string, String[] strings, boolean[] blns) {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void flush() throws IOException {
- throw new UnsupportedOperationException();
- }
-
- private static URI pageURL(String protocol, HttpServer server, final
String page) {
- int port = server.getPort();
- try {
- return new URI(protocol + "://localhost:" + port + page);
- } catch (URISyntaxException ex) {
- throw new IllegalStateException(ex);
- }
- }
-
- @Override
- public final void displayPage(URL page, Runnable onPageLoad) {
+ public void displayPage(HTMLViewerSpi.Context ctx) {
try {
- this.onPageLoad = onPageLoad;
- this.server = serverProvider.get();
- int from = 8080;
- int to = 65535;
- int port = config.getPort();
- if (port != -1) {
- from = to = port;
- }
- server.init(from, to);
-
- String prefix = "/" + new UUID(config.random.nextLong(),
config.random.nextLong()).toString();
-
- this.server.addHttpHandler(new RootPage(page, prefix), prefix);
- server.start();
-
- show(pageURL("http", server, prefix + "/"));
+ this.onPageLoad = () -> {
+ Buttons.registerCloseWindow();
+ try {
+ ctx.onPageLoad();
+ } catch (Exception ex) {
+ Exceptions.printStackTrace(ex);
+ }
+ };
+ this.id = UUID.randomUUID().toString();
+ Server.SESSIONS.put(this.id, new Command(this));
+ this.config.getBrowser().accept(new Config.Page(this.id,
getText(ctx.getPage(), this.id), getResources(ctx.getResources())));
} catch (IOException ex) {
Logger.getLogger(Browser.class.getName()).log(Level.SEVERE, null,
ex);
}
}
- /** Parameters to configure {@link Browser}.
- * Create an instance and pass it
- * to {@link
Browser#Browser(org.netbeans.html.presenters.browser.Browser.Config) }
- * constructor.
- */
- public static final class Config implements Cloneable {
- private Consumer<URI> browser;
- Integer port;
- boolean debug =
Boolean.getBoolean("com.dukescript.presenters.browserDebug");
- Random random = new Random();
-
- /**
- * Default constructor.
- */
- public Config() {
- command(null);
- }
-
- private Config(Config copy) {
- this.browser = copy.browser;
- this.port = copy.port;
- this.debug = copy.debug;
- this.random = copy.random;
- }
-
- @Override
- public Config clone() {
- return new Config(this);
- }
+ @Override
+ public void close() throws IOException {
+ Server.SESSIONS.remove(this.id);
+ }
- /** The command to use when invoking a browser. Possible values:
- * <ul>
- * <li>
- * <b>GTK</b> - use Gtk WebKit implementation. Requires presence of
appropriate native libraries
- * </li>
- * <li>
- * <b>AWT</b> - use Desktop.browse(java.net.URI) to launch a browser
- * </li>
- * <li>
- * <b>NONE</b> - just launches the server, useful together with
{@link #port(int)} to specify a fixed port to open the server at
- * </li>
- * <li>
- * any other value is interpreted as a command which is then launched
on a command line with one parameter - the URL to connect to
- * </li>
- * </ul>
- *
- * @param executable browser to execute
- * @return this instance
- */
- public Config command(String executable) {
- this.browser = (page) -> {
- String impl = executable;
- if (impl == null) {
- impl =
System.getProperty("com.dukescript.presenters.browser"); // NOI18N
+ private String getText(URL page, String id) throws IOException {
+ StringBuilder sb = new StringBuilder();
+ try (Reader is = new InputStreamReader(page.openStream())) {
+ int state = id != null ? 0 : 1000;
+ for (;;) {
+ int ch = is.read();
+ if (ch == -1) {
+ break;
}
- if ("none".equalsIgnoreCase(impl)) { // NOI18N
- return;
+ char lower = Character.toLowerCase((char)ch);
+ switch (state) {
+ case 1000: break;
+ case 0: if (lower == '<') state = 1; break;
+ case 1: if (lower == 'b') state = 2;
+ else if (lower != ' ' && lower != '\n') state = 0;
+ break;
+ case 2: if (lower == 'o') state = 3; else state = 0; break;
+ case 3: if (lower == 'd') state = 4; else state = 0; break;
+ case 4: if (lower == 'y') state = 5; else state = 0; break;
+ case 5: if (lower == '>') state = 500;
+ else if (lower != ' ' && lower != '\n') state = 0;
+ break;
+ }
+ sb.append((char)ch);
+ if (state == 500) {
+ emitScript(sb, id);
+ state = 1000;
}
- // Show.show(impl, page); TBD
- };
- return this;
- }
- public Config browser(Consumer<URI> urlOpener) {
- this.browser = urlOpener;
- return this;
- }
-
- /** The port to start the server at.
- * By default a random port is selected.
- * @param port the port
- * @return this instance
- */
- public Config port(int port) {
- this.port = port;
- return this;
- }
-
- /** Enable or disable debugging. The default value is taken from a
property
- * {@code com.dukescript.presenters.browserDebug}. If the property is
- * not specified, then the default value is {@code false}.
- *
- * @param debug true or false
- * @return this instance
- * @since 1.8
- */
- public Config debug(boolean debug) {
- this.debug = debug;
- return this;
- }
-
- final Consumer<URI> getBrowser() {
- return browser;
- }
-
- final int getPort() {
- if (port != null) {
- return port;
}
- String browserPort =
System.getProperty("com.dukescript.presenters.browserPort"); // NOI18N
- try {
- return Integer.parseInt(browserPort);
- } catch (NumberFormatException ex) {
- return -1;
+ if (state != 1000) {
+ emitScript(sb, id);
}
}
+ return sb.toString();
}
- static <Response> void cors(HttpServer<?, Response, ?, ?> s, Response r) {
- s.setCharacterEncoding(r, "UTF-8");
- s.addHeader(r, "Access-Control-Allow-Origin", "*");
- s.addHeader(r, "Access-Control-Allow-Credentials", "true");
- s.addHeader(r, "Access-Control-Allow-Headers", "Content-Type");
- s.addHeader(r, "Access-Control-Allow-Methods", "GET, POST, DELETE,
PUT");
+ private void emitScript(StringBuilder sb, String id) throws IOException {
+ sb.append("<script id='exec' type='text/javascript'>\n"
+ + "(function () {\n"
+ + " const self = this;\n"
+ + " this.vscode = acquireVsCodeApi();\n"
Review Comment:
Fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists