Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First Dart Macro change #903

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -119,6 +119,9 @@ public final class DartAnalysisServerService implements Disposable {
private static final long ANALYSIS_IN_TESTS_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
private static final long TESTS_TIMEOUT_COEFF = 10;

// LSP over Legacy Dart Analysis Protocol
public static final long LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT = TimeUnit.SECONDS.toMillis(50);

private static final Logger LOG = Logger.getInstance(DartAnalysisServerService.class);

private static final int DEBUG_LOG_CAPACITY = 30;
@@ -2041,6 +2044,40 @@ public void onError(final RequestError error) {
return resultRef.get();
}

//
// LSP over Legacy Dart Analysis Server protocols
//
@Nullable
public String lspMessage_dart_textDocumentContent(@NotNull final String uri) {
final RemoteAnalysisServerImpl server = myServer;
if (server == null) {
return null;
}

final Ref<String> resultRef = new Ref<>();
final CountDownLatch latch = new CountDownLatch(1);
server.lspMessage_dart_textDocumentContent(uri, new DartLSPTextDocumentContentConsumer() {
@Override
public void computedDocumentContents(String contents) {
resultRef.set(contents);
latch.countDown();
}

@Override
public void onError(final RequestError error) {
logError("lspMessage_dart_textDocumentContent()", uri, error);
latch.countDown();
}
});

awaitForLatchCheckingCanceled(server, latch, LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT);

if (latch.getCount() > 0) {
logTookTooLongMessage("lspMessage_dart_textDocumentContent", LSP_MESSAGE_TEXT_DOCUMENT_CONTENT_TIMEOUT, uri);
}
return resultRef.get();
}

private void startServer(@NotNull final DartSdk sdk) {
if (DartPubActionBase.isInProgress()) return; // DartPubActionBase will start the server itself when finished

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024, the Dart project authors.
*
* Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.eclipse.org/legal/epl-v10.html
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.dart.server;

import org.dartlang.analysis.server.protocol.RequestError;

public interface DartLSPTextDocumentContentConsumer extends Consumer {

public void computedDocumentContents(String contents);

/**
* If the file contents can't be sent back, some {@link RequestError} is passed back instead.
*
* @param requestError the reason why a result was not passed back
*/
public void onError(RequestError requestError);
}
Original file line number Diff line number Diff line change
@@ -606,6 +606,14 @@ public void received() {
stopServer();
}

//
// LSP over Legacy Dart Analysis Server protocol
//
public void lspMessage_dart_textDocumentContent(String uri, DartLSPTextDocumentContentConsumer consumer) {
String id = generateUniqueId();
sendRequestToServer(id, RequestUtilities.generateLSPMessage_dart_textDocumentContent(id, uri), consumer);
}

/**
* Starts the analysis server.
*
@@ -929,6 +937,12 @@ else if (consumer instanceof BasicConsumer) {
else if (consumer instanceof JsonConsumer) {
((JsonConsumer)consumer).onResponse(resultObject, requestError);
}
//
// LSP over Legacy DAS Dart Analysis Server protocol
//
else if (consumer instanceof DartLSPTextDocumentContentConsumer) {
new LSPDartTextDocumentContentProcessor((DartLSPTextDocumentContentConsumer)consumer).process(resultObject, requestError);
}

synchronized (consumerMapLock) {
consumerMap.remove(idString);
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (c) 2024, the Dart project authors.
*
* Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.eclipse.org/legal/epl-v10.html
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.dart.server.internal.remote.processor;

import com.google.dart.server.DartLSPTextDocumentContentConsumer;
import com.google.gson.JsonObject;

import org.dartlang.analysis.server.protocol.RequestError;

/**
* Instances of {@code LSPDartTextDocumentContentProcessor} translate JSON result objects for a given
* {@link LSPDartTextDocumentContentConsumer}.
*
* @coverage dart.server.remote
*/
public class LSPDartTextDocumentContentProcessor extends ResultProcessor {

private final DartLSPTextDocumentContentConsumer consumer;

public LSPDartTextDocumentContentProcessor(DartLSPTextDocumentContentConsumer consumer) {
this.consumer = consumer;
}

public void process(JsonObject resultObject, RequestError requestError) {
if (resultObject != null) {
// Example: {"lspResponse":{"id":"1","jsonrpc":"2.0","result":{"content":"file contents"}}}
JsonObject lspResponse = resultObject.getAsJsonObject("lspResponse");
JsonObject innerResultObject = lspResponse.getAsJsonObject("result");
final String contents = innerResultObject.get("content").getAsString();
consumer.computedDocumentContents(contents);
}
if (requestError != null) {
consumer.onError(requestError);
}
}
}
Original file line number Diff line number Diff line change
@@ -1199,6 +1199,46 @@ private static String getRequestMethod(JsonObject request) {
return null;
}

//
// LSP over Legacy DAS (Dart Analysis Server) protocol below
//
public static final String LSP_DART_TEXT_DOCUMENT_CONTENT = "dart/textDocumentContent";

private static final String LSP_HANDLE = "lsp.handle";

public static final String LSP_JSONRPC = "jsonrpc";

public static final String LSP_JSONROC_VERSION = "2.0";

public static final String LSP_MESSAGE = "lspMessage";

/**
* Generate and return a LSP over Legacy DAS request.
*
* Example:
* {"id":"2","method":"lsp.handle","params":
* {"lspMessage":
* {"id":0,"jsonrpc":"2.0","method":"dart/textDocumentContent","params":
* {"position":{"character":7,"line":1},
* "textDocument":{"uri":"some-uri"}}}}}
*/
private static JsonObject generateLSPMessage(String idValue, String lspMethod, JsonObject lspParams) {
JsonObject lspMessageParams = new JsonObject();
lspMessageParams.addProperty(ID, idValue);
lspMessageParams.addProperty(LSP_JSONRPC, LSP_JSONROC_VERSION);
lspMessageParams.addProperty(METHOD, lspMethod);
lspMessageParams.add(PARAMS, lspParams);
JsonObject lspMessage = new JsonObject();
lspMessage.add(LSP_MESSAGE, lspMessageParams);
return buildJsonObjectRequest(idValue, LSP_HANDLE, lspMessage);
}

public static JsonObject generateLSPMessage_dart_textDocumentContent(String idValue, String uri) {
JsonObject lspParams = new JsonObject();
lspParams.addProperty("uri", uri);
return generateLSPMessage(idValue, LSP_DART_TEXT_DOCUMENT_CONTENT, lspParams);
}

private RequestUtilities() {
}
}