-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add completions for for build files (#193)
The server now can provide completions for smithy-build.json and .smithy-project.json. The implementation works roughly the same as other node-like completions, using a new builtins model that specifies the structure of the build files. I also had to override the completion item mapper to make sure it wrapped object keys in strings, which is necessary in json.
- Loading branch information
1 parent
fcaa7d5
commit 6c4eef4
Showing
8 changed files
with
532 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/software/amazon/smithy/lsp/language/BuildCompletionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package software.amazon.smithy.lsp.language; | ||
|
||
import java.util.List; | ||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.eclipse.lsp4j.CompletionParams; | ||
import org.eclipse.lsp4j.Position; | ||
import org.eclipse.lsp4j.Range; | ||
import software.amazon.smithy.lsp.document.DocumentId; | ||
import software.amazon.smithy.lsp.project.BuildFile; | ||
import software.amazon.smithy.lsp.project.Project; | ||
import software.amazon.smithy.lsp.syntax.NodeCursor; | ||
import software.amazon.smithy.model.shapes.Shape; | ||
|
||
/** | ||
* Handles completions requests for {@link BuildFile}s. | ||
*/ | ||
public final class BuildCompletionHandler { | ||
private final Project project; | ||
private final BuildFile buildFile; | ||
|
||
public BuildCompletionHandler(Project project, BuildFile buildFile) { | ||
this.project = project; | ||
this.buildFile = buildFile; | ||
} | ||
|
||
/** | ||
* @param params The request params | ||
* @return A list of possible completions | ||
*/ | ||
public List<CompletionItem> handle(CompletionParams params) { | ||
Position position = CompletionHandler.getTokenPosition(params); | ||
DocumentId id = buildFile.document().copyDocumentId(position); | ||
Range insertRange = CompletionHandler.getInsertRange(id, position); | ||
|
||
Shape buildFileShape = Builtins.getBuildFileShape(buildFile.type()); | ||
|
||
if (buildFileShape == null) { | ||
return List.of(); | ||
} | ||
|
||
NodeCursor cursor = NodeCursor.create( | ||
buildFile.getParse().value(), | ||
buildFile.document().indexOfPosition(position) | ||
); | ||
NodeSearch.Result searchResult = NodeSearch.search(cursor, Builtins.MODEL, buildFileShape); | ||
var candidates = CompletionCandidates.fromSearchResult(searchResult); | ||
|
||
var context = CompleterContext.create(id, insertRange, project) | ||
.withExclude(searchResult.getOtherPresentKeys()); | ||
var mapper = new SimpleCompleter.BuildFileMapper(context); | ||
|
||
return new SimpleCompleter(context, mapper).getCompletionItems(candidates); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/resources/software/amazon/smithy/lsp/language/build.smithy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
$version: "2.0" | ||
|
||
namespace smithy.lang.server | ||
|
||
structure SmithyProjectJson { | ||
sources: Strings | ||
imports: Strings | ||
outputDirectory: String | ||
dependencies: ProjectDependencies | ||
} | ||
|
||
list ProjectDependencies { | ||
member: ProjectDependency | ||
} | ||
|
||
structure ProjectDependency { | ||
name: String | ||
|
||
@required | ||
path: String | ||
} | ||
|
||
structure SmithyBuildJson { | ||
@required | ||
version: SmithyBuildVersion | ||
|
||
outputDirectory: String | ||
sources: Strings | ||
imports: Strings | ||
projections: Projections | ||
plugins: Plugins | ||
ignoreMissingPlugins: Boolean | ||
maven: Maven | ||
} | ||
|
||
@default("1") | ||
string SmithyBuildVersion | ||
|
||
map Projections { | ||
key: String | ||
value: Projection | ||
} | ||
|
||
structure Projection { | ||
abstract: Boolean | ||
imports: Strings | ||
transforms: Transforms | ||
plugins: Plugins | ||
} | ||
|
||
map Plugins { | ||
key: String | ||
value: Document | ||
} | ||
|
||
list Transforms { | ||
member: Transform | ||
} | ||
|
||
structure Transform { | ||
@required | ||
name: String | ||
|
||
args: TransformArgs | ||
} | ||
|
||
structure TransformArgs { | ||
} | ||
|
||
structure Maven { | ||
dependencies: Strings | ||
repositories: MavenRepositories | ||
} | ||
|
||
list MavenRepositories { | ||
member: MavenRepository | ||
} | ||
|
||
structure MavenRepository { | ||
@required | ||
url: String | ||
|
||
httpCredentials: String | ||
proxyHost: String | ||
proxyCredentials: String | ||
} | ||
|
||
list Strings { | ||
member: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.