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

Use smithy official formatter #139

Closed
Closed
Show file tree
Hide file tree
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
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
Loading