Skip to content

Commit

Permalink
Use smithy official formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
daddykotex committed Jan 18, 2024
1 parent fea1fd7 commit dfca5e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ dependencies {
implementation "org.eclipse.lsp4j:org.eclipse.lsp4j:0.14.0"
implementation "software.amazon.smithy:smithy-build:[smithyVersion, 2.0["
implementation "software.amazon.smithy:smithy-cli:[smithyVersion, 2.0["
implementation "software.amazon.smithy:smithy-syntax:[smithyVersion, 2.0["
implementation "software.amazon.smithy:smithy-model:[smithyVersion, 2.0["
implementation 'com.disneystreaming.smithy:smithytranslate-formatter-jvm-java-api:0.3.10'

// Use JUnit test framework
testImplementation "junit:junit:4.13"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@
import org.eclipse.lsp4j.jsonrpc.messages.Either;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.TextDocumentService;
import smithyfmt.Formatter;
import smithyfmt.Result;
import software.amazon.smithy.cli.dependencies.DependencyResolver;
import software.amazon.smithy.cli.dependencies.FileCacheResolver;
import software.amazon.smithy.cli.dependencies.MavenDependencyResolver;
Expand All @@ -91,6 +89,7 @@
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.knowledge.NeighborProviderIndex;
import software.amazon.smithy.model.loader.IdlTokenizer;
import software.amazon.smithy.model.loader.ParserUtils;
import software.amazon.smithy.model.neighbor.Walker;
import software.amazon.smithy.model.shapes.Shape;
Expand All @@ -99,6 +98,8 @@
import software.amazon.smithy.model.shapes.SmithyIdlModelSerializer;
import software.amazon.smithy.model.validation.ValidatedResult;
import software.amazon.smithy.model.validation.ValidationEvent;
import software.amazon.smithy.syntax.Formatter;
import software.amazon.smithy.syntax.TokenTree;
import software.amazon.smithy.utils.SimpleParser;

public class SmithyTextDocumentService implements TextDocumentService {
Expand Down Expand Up @@ -697,17 +698,20 @@ public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormatting
);
if (content.isPresent()) {
SmartInput input = content.get();
final Result result = Formatter.format(input.getInput());
final Range fullRange = input.getRange();
if (result.isSuccess() && !result.getValue().equals(input.getInput())) {
return Utils.completableFuture(Collections.singletonList(new TextEdit(
fullRange,
result.getValue()
)));
} else if (!result.isSuccess()) {
LspLog.println("Failed to format: " + result.getError());
return emptyResult;
} else {
try {
String formatted = Formatter.format(parse(file.toPath().toString(), input.getInput()));
final Range fullRange = input.getRange();
if (!formatted.equals(input.getInput())) {
return Utils.completableFuture(Collections.singletonList(new TextEdit(
fullRange,
formatted
)));
} else {
return emptyResult;
}
} catch (Throwable ex) {
LspLog.println("Failed to format");
LspLog.println(ex);
return emptyResult;
}
} else {
Expand All @@ -716,6 +720,11 @@ public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormatting
}
}

private TokenTree parse(String fileName, String contents) {
IdlTokenizer tokenizer = IdlTokenizer.create(fileName, contents);
return TokenTree.of(tokenizer);
}

private File fileUri(TextDocumentIdentifier tdi) {
return fileFromUri(tdi.getUri());
}
Expand Down

0 comments on commit dfca5e1

Please sign in to comment.