Skip to content

Commit

Permalink
fix: reparsing schema when moving rank profile to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
Mangern committed Aug 15, 2024
1 parent d06777e commit 5a64d69
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ public void didOpen(DidOpenTextDocumentParams params) {
@Override
public void didChange(DidChangeTextDocumentParams params) {
var document = params.getTextDocument();

SchemaDocumentScheduler scheduler = eventContextCreator.scheduler;

var contentChanges = params.getContentChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ private Optional<CodeAction> getMoveRankProfile(SchemaNode node, EventCodeAction
action.setTitle("Move '" + rankProfileName + "' to separate file");
action.setKind(CodeActionKind.RefactorRewrite);
action.setEdit(edit);
action.setCommand(CommandRegistry.createLSPCommand(CommandType.DOCUMENT_OPEN, List.of(rankProfileURI)));
//action.setCommand(CommandRegistry.createLSPCommand(CommandType.DOCUMENT_OPEN, List.of(rankProfileURI)));
//action.setCommand(CommandRegistry.createLSPCommand(CommandType.DOCUMENT_PARSE, List.of(context.document.getFileURI())));
action.setCommand(CommandRegistry.createLSPCommand(CommandType.COMMAND_LIST, List.of(
CommandRegistry.createLSPCommand(CommandType.DOCUMENT_PARSE, List.of(context.document.getFileURI())),
CommandRegistry.createLSPCommand(CommandType.DOCUMENT_OPEN, List.of(rankProfileURI))
)));
return Optional.of(action);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.eclipse.lsp4j.ExecuteCommandParams;

import ai.vespa.schemals.lsp.command.commandtypes.DocumentOpen;
import ai.vespa.schemals.lsp.command.commandtypes.DocumentParse;
import ai.vespa.schemals.lsp.command.commandtypes.CommandList;
import ai.vespa.schemals.lsp.command.commandtypes.SchemaCommand;

/**
Expand All @@ -22,6 +24,14 @@ public enum CommandType implements GenericCommandType {
DOCUMENT_OPEN {
public String title() { return "Open document"; }
public SchemaCommand construct() { return new DocumentOpen(); }
},
DOCUMENT_PARSE {
public String title() { return "Parse document"; }
public SchemaCommand construct() { return new DocumentParse(); }
},
COMMAND_LIST {
public String title() { return "Command list"; }
public SchemaCommand construct() { return new CommandList(); }
}
}

Expand All @@ -34,7 +44,7 @@ public static Optional<SchemaCommand> getCommand(ExecuteCommandParams params) {
CommandType commandType = CommandType.valueOf(params.getCommand());
SchemaCommand command = commandType.construct();

if (command.getArity() != params.getArguments().size()) return Optional.empty();
if (command.getArity() != -1 && command.getArity() != params.getArguments().size()) return Optional.empty();
if (!command.setArguments(params.getArguments())) return Optional.empty();
return Optional.of(command);
} catch(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ public class ExecuteCommand {
public static Object executeCommand(EventExecuteCommandContext context) {
Optional<SchemaCommand> command = CommandRegistry.getCommand(context.params);

if (command.isEmpty()) {
for (Object obj : context.params.getArguments()) {
context.logger.info(obj.getClass().toString() + " ||| " + obj.toString());
}
}

command.ifPresent(cmd -> cmd.execute(context));
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ai.vespa.schemals.lsp.command.commandtypes;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.eclipse.lsp4j.ExecuteCommandParams;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

import ai.vespa.schemals.lsp.command.CommandRegistry;
import ai.vespa.schemals.context.EventExecuteCommandContext;

/**
* CommandList
* Represents a chain of commands to be executed one after another.
*/
public class CommandList implements SchemaCommand {

List<SchemaCommand> commandsToExecute = new ArrayList<>();

@Override
public int getArity() {
return -1;
}

@Override
public boolean setArguments(List<Object> arguments) {
commandsToExecute.clear();

for (Object object : arguments) {
if (!(object instanceof JsonObject))return false;

JsonObject jsonObject = (JsonObject)object;

if (!jsonObject.has("command") || !jsonObject.get("command").isJsonPrimitive())return false;
if (!jsonObject.has("arguments") || !jsonObject.get("arguments").isJsonArray())return false;

var params = new ExecuteCommandParams(jsonObject.get("command").getAsString(), new ArrayList<Object>(((JsonArray)jsonObject.get("arguments")).asList()));
Optional<SchemaCommand> command = CommandRegistry.getCommand(params);
if (command.isEmpty()) return false;
commandsToExecute.add(command.get());
}
return true;
}

@Override
public void execute(EventExecuteCommandContext context) {
for (SchemaCommand cmd : commandsToExecute) {
cmd.execute(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

/**
* OpenDocument
* Kind of a reflection: client sends "Open document with fileURI", and server responds by telling the client to open it.
* Reason for this: Used in Code Actions to make file automatically open with new edit.
*/
public class DocumentOpen implements SchemaCommand {
private String fileURI;
Expand All @@ -18,9 +20,7 @@ public class DocumentOpen implements SchemaCommand {
public void execute(EventExecuteCommandContext context) {
if (fileURI == null)
return;
context.logger.info("Show document: " + fileURI);
ShowDocumentResult result = context.messageHandler.showDocument(fileURI).join();
context.logger.info("Result: " + result.toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package ai.vespa.schemals.lsp.command.commandtypes;

import java.util.List;

import com.google.gson.JsonPrimitive;

import ai.vespa.schemals.context.EventExecuteCommandContext;
import ai.vespa.schemals.schemadocument.DocumentManager;

/**
* DocumentParse
*/
public class DocumentParse implements SchemaCommand {
private String fileURI;

@Override
public int getArity() {
return 1;
}

@Override
public boolean setArguments(List<Object> arguments) {
assert arguments.size() == getArity();

if (!(arguments.get(0) instanceof JsonPrimitive))
return false;

JsonPrimitive arg = (JsonPrimitive) arguments.get(0);
this.fileURI = arg.getAsString();
return true;
}

@Override
public void execute(EventExecuteCommandContext context) {
DocumentManager document = context.scheduler.getDocument(fileURI);
if (document == null) return;
document.reparseContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public void updateFileContent(String content) {
var result = parseContent(context);

diagnosticsHandler.publishDiagnostics(this.fileURI, result.diagnostics());
logger.info("CST FOR RANK PROFILE " + this.fileURI);
if (result.CST().isPresent()) {
this.CST = result.CST().get();
lexer.setCST(CST);
Expand Down

0 comments on commit 5a64d69

Please sign in to comment.