Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
malinthar committed Jul 23, 2023
1 parent 3688fc3 commit 8368bc5
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@
/**
* Represents the editor used by the end user. An editor consists of a set of open tabs.
*
* @since 2.0.0
* @since 2201.8.0
*/
public class Editor {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,7 +29,7 @@
/**
* A custom output stream to consume messages sent from LS to the LS client side.
*
* @since 2.0.0
* @since 2201.8.0
*/
class EditorOutputStream extends ByteArrayOutputStream {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
Expand All @@ -39,7 +40,7 @@
* The main class to simulate the behavior of language server. Similarly to how vscode client use LSP to send different
* updates, this sends similar messages via JSON RPC to the language server.
*
* @since 2.0.0
* @since 2201.8.0
*/
public class EditorSimulator {

Expand All @@ -48,7 +49,7 @@ public class EditorSimulator {
private static final String PROP_DURATION = "ls.simulation.duration";
public static final String PROP_SOURCE_DIR = "ls.simulation.src";

private static final Random random = new Random();
private static final SecureRandom random = new SecureRandom();

public static void main(String[] args) throws IOException {
try {
Expand Down Expand Up @@ -140,6 +141,7 @@ public static void run() throws IOException {
try {
Thread.sleep(60 * 1000L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.warn("Interrupted editing", e);
break;
}
Expand All @@ -149,6 +151,7 @@ public static void run() throws IOException {
int sleepSecs = 1 + random.nextInt(5);
Thread.sleep(sleepSecs * 1000L);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.warn("Interrupted simulation", e);
break;
}
Expand All @@ -170,7 +173,6 @@ public static Generators.Type getRandomGenerator() {
.collect(Collectors.toList());

// Get random generator
Generators.Type type = types.get(random.nextInt(types.size()));
return type;
return types.get(random.nextInt(types.size()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -39,16 +39,16 @@
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.SecureRandom;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.concurrent.CompletableFuture;

/**
* Represents a tab in the {@link Editor}. Simulates the behavior of cursor and current text in the document.
*
* @since 2.0.0
* @since 2201.8.0
*/
public class EditorTab {

Expand All @@ -61,7 +61,7 @@ public class EditorTab {
private TextDocument textDocument;
private Position cursor;

private final Random random = new Random();
private final SecureRandom random = new SecureRandom();
private final PrintWriter writer = new PrintWriter(System.out, true, Charset.defaultCharset());

public EditorTab(Path filePath, Endpoint endpoint, BallerinaLanguageServer languageServer) {
Expand Down Expand Up @@ -103,7 +103,6 @@ public void type(String content) {
logger.error("Caught error in didChange", t);
}
cursor(newLinePos.line(), newLinePos.offset());
// logger.info("Added char: {} and cursor advanced to: {}", typedChar, newLinePos);

if (i % 10 == 0) {
float completionPercentage = ((float) i / (float) content.length()) * 100;
Expand All @@ -123,6 +122,7 @@ public void type(String content) {
try {
Thread.sleep(100 + random.nextInt(300));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
logger.error("Interrupted", e);
break;
}
Expand All @@ -134,8 +134,9 @@ public void type(String content) {
while (isDocumentNotInSync()) {
logger.info("Document out of sync. Waiting 30 seconds and syncing...");
try {
Thread.sleep(30 * 1000);
Thread.sleep(30 * (long) 1000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
}
TestUtil.didChangeDocument(this.endpoint, this.filePath, textDocument.toString());
Expand Down Expand Up @@ -165,8 +166,9 @@ public void completions() {
String completionResponse = TestUtil.getCompletionResponse(filePath.toString(), cursor, endpoint, "");
JsonObject json = JsonParser.parseString(completionResponse).getAsJsonObject();
boolean hasError = false;
if (json.has("result") && json.get("result").isJsonObject()) {
JsonObject result = json.getAsJsonObject("result");
String resultProp = "result";
if (json.has(resultProp) && json.get(resultProp).isJsonObject()) {
JsonObject result = json.getAsJsonObject(resultProp);
if (!result.has("left") || !result.get("left").isJsonArray()) {
hasError = true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,7 +23,7 @@
/**
* Class code snippet generator.
*
* @since 2.0.0
* @since 2201.8.0
*/
@JavaSPIService("org.ballerinalang.langserver.org.ballerinalang.langserver.simulator.generators.CodeSnippetGenerator")
public class ClassGenerator extends CodeSnippetGenerator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,18 +15,18 @@
*/
package org.ballerinalang.langserver.simulator.generators;

import java.security.SecureRandom;
import java.util.List;
import java.util.Random;

/**
* Abstract implementation of code snippet generator for the LS simulator.
*
* @since 2.0.0
* @since 2201.8.0
*/
public abstract class CodeSnippetGenerator {

protected final List<String> primitiveTypes = List.of("string", "int", "float", "decimal", "boolean");
protected Random random = new Random();
protected SecureRandom random = new SecureRandom();

public abstract String generate();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@
/**
* Function code snippet generator.
*
* @since 2.0.0
* @since 2201.8.0
*/
@JavaSPIService("org.ballerinalang.langserver.org.ballerinalang.langserver.simulator.generators.CodeSnippetGenerator")
public class FunctionGenerator extends CodeSnippetGenerator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,8 +18,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.EnumMap;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -28,14 +27,14 @@
/**
* Factory to access {@link CodeSnippetGenerator}s.
*
* @since 2.0.0
* @since 2201.8.0
*/
public class Generators {

private static final Logger logger = LoggerFactory.getLogger(Generators.class);
private static final String PROP_SKIPPED_GENERATORS = "ls.simulation.skipGenerators";
private static final Generators instance = new Generators();
private final Map<Type, CodeSnippetGenerator> generators;
private final EnumMap<Generators.Type, CodeSnippetGenerator> GENERATORS;

private Generators() {
// Get skipped generators
Expand All @@ -47,11 +46,11 @@ private Generators() {
logger.info("Skipping generators of type: " + skippedGenerators);

// Load generators
generators = new HashMap<>();
GENERATORS = new EnumMap<>(Generators.Type.class);
ServiceLoader.load(CodeSnippetGenerator.class)
.forEach(generator -> {
if (!skippedGenerators.contains(generator.type())) {
generators.put(generator.type(), generator);
GENERATORS.put(generator.type(), generator);
}
});
}
Expand All @@ -63,15 +62,15 @@ private Generators() {
* @return Generated code snippet.
*/
public static String generate(Type type) {
if (getInstance().generators.containsKey(type)) {
return instance.generators.get(type).generate();
if (getInstance().GENERATORS.containsKey(type)) {
return instance.GENERATORS.get(type).generate();
}

return "";
}

public static <T> T getGenerator(Type type) {
return (T) instance.generators.get(type);
return (T) instance.GENERATORS.get(type);
}

public static Generators getInstance() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,11 +24,12 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Import statement snippet generator.
*
* @since 2201.1.1
* @since 2201.8.0
*/
@JavaSPIService("org.ballerinalang.langserver.org.ballerinalang.langserver.simulator.generators.CodeSnippetGenerator")
public class ImportStatementGenerator extends CodeSnippetGenerator {
Expand All @@ -45,9 +46,8 @@ public String generate() {
Path path = Paths.get(projectPath);
Path modulesPath = path.resolve("modules");
if (Files.exists(modulesPath)) {
try {
List<String> imports = Files.list(modulesPath)
.filter(Files::isDirectory)
try (Stream<Path> paths = Files.list(modulesPath)) {
List<String> imports = paths.filter(Files::isDirectory)
.map(p -> "import " + PACKAGE_NAME + "." + p.getFileName() + ";")
.collect(Collectors.toList());
if (!imports.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@
/**
* Match statement code snippet generator.
*
* @since 2.0.0
* @since 2201.8.0
*/
@JavaSPIService("org.ballerinalang.langserver.org.ballerinalang.langserver.simulator.generators.CodeSnippetGenerator")
public class MatchStatementGenerator extends CodeSnippetGenerator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@
/**
* Service code snippet generator.
*
* @since 2.0.0
* @since 2201.8.0
*/
@JavaSPIService("org.ballerinalang.langserver.org.ballerinalang.langserver.simulator.generators.CodeSnippetGenerator")
public class ServiceGenerator extends CodeSnippetGenerator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,7 +20,7 @@
/**
* Statement code snippet generator.
*
* @since 2.0.0
* @since 2201.8.0
*/
@JavaSPIService("org.ballerinalang.langserver.org.ballerinalang.langserver.simulator.generators.CodeSnippetGenerator")
public class StatementGenerator extends CodeSnippetGenerator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,7 @@
/**
* Type definition code snippet generator.
*
* @since 2.0.0
* @since 2201.8.0
*/
@JavaSPIService("org.ballerinalang.langserver.org.ballerinalang.langserver.simulator.generators.CodeSnippetGenerator")
public class TypeDefinitionGenerator extends CodeSnippetGenerator {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, WSO2 Inc. (http://wso2.com) All Rights Reserved.
* Copyright (c) 2023, WSO2 LLC. (http://wso2.com) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down

0 comments on commit 8368bc5

Please sign in to comment.