-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support searching types in the search api
- Loading branch information
Showing
22 changed files
with
2,119 additions
and
57 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
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
132 changes: 132 additions & 0 deletions
132
...tor-core/src/main/java/io/ballerina/flowmodelgenerator/core/search/TypeSearchCommand.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,132 @@ | ||
/* | ||
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com) | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.ballerina.flowmodelgenerator.core.search; | ||
|
||
import io.ballerina.flowmodelgenerator.core.model.AvailableNode; | ||
import io.ballerina.flowmodelgenerator.core.model.Category; | ||
import io.ballerina.flowmodelgenerator.core.model.Codedata; | ||
import io.ballerina.flowmodelgenerator.core.model.Item; | ||
import io.ballerina.flowmodelgenerator.core.model.Metadata; | ||
import io.ballerina.flowmodelgenerator.core.model.NodeKind; | ||
import io.ballerina.modelgenerator.commons.CommonUtils; | ||
import io.ballerina.modelgenerator.commons.SearchResult; | ||
import io.ballerina.projects.Module; | ||
import io.ballerina.tools.text.LineRange; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Represents a command to search for types within a module. This class extends SearchCommand and provides functionality | ||
* to search for both project-specific and library types. | ||
* | ||
* <p> | ||
* The search includes: | ||
* <li>Types within the current project/module </li> | ||
* <li>Imported types from dependencies</li> | ||
* <li>Available types from the standard library (if enabled)</li> | ||
* | ||
* <p>The search results are organized into different categories:</p> | ||
* <li>CURRENT_INTEGRATION: Types from the current project</li> | ||
* <li>IMPORTED_TYPES: Types from imported modules</li> | ||
* <li>AVAILABLE_TYPES: Types available but not imported (optional)</li> | ||
* </p> | ||
* | ||
* @see SearchCommand | ||
* @since 2.0.0 | ||
*/ | ||
class TypeSearchCommand extends SearchCommand { | ||
|
||
private final List<String> moduleNames; | ||
|
||
public TypeSearchCommand(Module module, LineRange position, Map<String, String> queryMap) { | ||
super(module, position, queryMap); | ||
|
||
// Obtain the imported module names | ||
module.getCompilation(); | ||
moduleNames = module.moduleDependencies().stream() | ||
.map(moduleDependency -> moduleDependency.descriptor().name().packageName().value()) | ||
.toList(); | ||
} | ||
|
||
@Override | ||
protected List<Item> defaultView() { | ||
List<SearchResult> searchResults = new ArrayList<>(); | ||
if (!moduleNames.isEmpty()) { | ||
searchResults.addAll(dbManager.searchTypesByPackages(moduleNames, limit, offset)); | ||
} | ||
buildLibraryNodes(searchResults, false); | ||
return rootBuilder.build().items(); | ||
} | ||
|
||
@Override | ||
protected List<Item> search() { | ||
List<SearchResult> typeSearchList = dbManager.searchTypes(query, limit, offset); | ||
buildLibraryNodes(typeSearchList, true); | ||
return rootBuilder.build().items(); | ||
} | ||
|
||
@Override | ||
protected Map<String, List<SearchResult>> fetchPopularItems() { | ||
// Return empty value as required | ||
return Collections.emptyMap(); | ||
} | ||
|
||
private void buildLibraryNodes(List<SearchResult> typeSearchList, boolean includeAllResults) { | ||
// Set the categories based on available flags | ||
Category.Builder importedTypesBuilder = rootBuilder.stepIn(Category.Name.IMPORTED_TYPES); | ||
Category.Builder availableTypesBuilder = null; | ||
if (includeAllResults && includeAvailableNodes) { | ||
availableTypesBuilder = rootBuilder.stepIn(Category.Name.AVAILABLE_TYPES); | ||
} | ||
|
||
// Add the library types | ||
for (SearchResult searchResult : typeSearchList) { | ||
SearchResult.Package packageInfo = searchResult.packageInfo(); | ||
|
||
// Skip if the module is not imported and we're not including all results | ||
boolean isImportedModule = moduleNames.contains(packageInfo.name()); | ||
if (!isImportedModule && !includeAllResults) { | ||
continue; | ||
} | ||
|
||
// Add the type to the respective category | ||
String icon = CommonUtils.generateIcon(packageInfo.org(), packageInfo.name(), packageInfo.version()); | ||
Metadata metadata = new Metadata.Builder<>(null) | ||
.label(searchResult.name()) | ||
.description(searchResult.description()) | ||
.icon(icon) | ||
.build(); | ||
Codedata codedata = new Codedata.Builder<>(null) | ||
.node(NodeKind.TYPEDESC) | ||
.org(packageInfo.org()) | ||
.module(packageInfo.name()) | ||
.symbol(searchResult.name()) | ||
.version(packageInfo.version()) | ||
.build(); | ||
Category.Builder builder = isImportedModule ? importedTypesBuilder : availableTypesBuilder; | ||
if (builder != null) { | ||
builder.stepIn(packageInfo.name(), "", List.of()) | ||
.node(new AvailableNode(metadata, codedata, true)); | ||
} | ||
} | ||
} | ||
} |
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
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
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
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
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.