Skip to content

Commit

Permalink
Merge pull request #20819 from nadeeshaan/completion-engine-revamp
Browse files Browse the repository at this point in the history
Add Sorting support For top Level Contexts
  • Loading branch information
nadeeshaan authored Feb 3, 2020
2 parents 898ae60 + 463c0de commit 487f603
Show file tree
Hide file tree
Showing 24 changed files with 1,035 additions and 1,172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ public static SnippetBlock getFunctionKeywordSnippet() {
SnippetType.KEYWORD);
}

/**
* Get resource Keyword Snippet Block.
*
* @return {@link SnippetBlock} Generated Snippet Block
*/
public static SnippetBlock getResourceKeywordSnippet() {
return new SnippetBlock(ItemResolverConstants.RESOURCE, "resource ", ItemResolverConstants.KEYWORD_TYPE,
SnippetType.KEYWORD);
}

/**
* Get Continue Statement Snippet Block.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public List<LSCompletionItem> getCompletions(LSContext ctx) throws LSCompletionE
}

completionItems.add(new SnippetCompletionItem(ctx, Snippet.KW_PUBLIC.get()));
completionItems.add(new SnippetCompletionItem(ctx, Snippet.KW_FUNCTION.get()));
completionItems.add(new SnippetCompletionItem(ctx, Snippet.KW_RESOURCE.get()));
completionItems.addAll(this.getResourceSnippets(ctx));
completionItems.add(new SnippetCompletionItem(ctx, Snippet.DEF_FUNCTION.get()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public TopLevelScopeProvider() {
@Override
public List<LSCompletionItem> getCompletions(LSContext ctx) throws LSCompletionException {
ArrayList<LSCompletionItem> completionItems = new ArrayList<>();

List<CommonToken> lhsTokens = ctx.get(SourcePruneKeys.LHS_TOKENS_KEY);
Optional<String> subRule = this.getSubRule(lhsTokens);
subRule.ifPresent(rule -> CompletionSubRuleParser.parseWithinCompilationUnit(rule, ctx));

if (this.inFunctionReturnParameterContext(ctx)) {
return this.getProvider(BallerinaParser.ReturnParameterContext.class).getCompletions(ctx);
}
Expand Down Expand Up @@ -126,7 +129,6 @@ private List<LSCompletionItem> getCompletionOnParameterContext(LSContext lsConte
@Override
public Optional<LSCompletionProvider> getContextProvider(LSContext ctx) {
List<Integer> lhsTokensTypes = ctx.get(SourcePruneKeys.LHS_DEFAULT_TOKEN_TYPES_KEY);
List<CommonToken> lhsTokens = ctx.get(SourcePruneKeys.LHS_TOKENS_KEY);
Boolean forcedRemoved = ctx.get(SourcePruneKeys.FORCE_REMOVED_STATEMENT_WITH_PARENTHESIS_KEY);
if (lhsTokensTypes == null || lhsTokensTypes.isEmpty() || (forcedRemoved != null && forcedRemoved)) {
return Optional.empty();
Expand All @@ -137,13 +139,11 @@ public Optional<LSCompletionProvider> getContextProvider(LSContext ctx) {
// Handle with the parser rule context
int serviceTokenIndex = lhsTokensTypes.indexOf(BallerinaParser.SERVICE);
int assignTokenIndex = lhsTokensTypes.indexOf(BallerinaParser.ASSIGN);
ParserRuleContext parserRuleContext = ctx.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);

if (serviceTokenIndex > -1 && assignTokenIndex == -1) {
return Optional.ofNullable(this.getProvider(BallerinaParser.ServiceDefinitionContext.class));
}
Optional<String> subRule = this.getSubRule(lhsTokens);
subRule.ifPresent(rule -> CompletionSubRuleParser.parseWithinCompilationUnit(rule, ctx));
ParserRuleContext parserRuleContext = ctx.get(CompletionKeys.PARSER_RULE_CONTEXT_KEY);

if (parserRuleContext != null) {
return Optional.ofNullable(this.getProvider(parserRuleContext.getClass()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public enum Snippet {

KW_FUNCTION(SnippetGenerator.getFunctionKeywordSnippet()),

KW_RESOURCE(SnippetGenerator.getResourceKeywordSnippet()),

KW_PUBLIC(SnippetGenerator.getPublicKeywordSnippet()),

KW_PRIVATE(SnippetGenerator.getPrivateKeywordSnippet()),
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.ballerinalang.langserver.completions.util.sorters.context.DefinitionContext;
import org.ballerinalang.langserver.completions.util.sorters.context.ImportDeclarationContext;
import org.ballerinalang.langserver.completions.util.sorters.scope.PackageScope;
import org.ballerinalang.langserver.completions.util.sorters.scope.ServiceScope;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -30,6 +31,7 @@
*/
public enum ItemSorters {
DEFAULT_ITEM_SORTER(new DefaultItemSorter()),
SERVICE_SCOPE_ITEM_SORTER(new ServiceScope()),
DEFINITION_CTX_ITEM_SORTER(new DefinitionContext()),
PACKAGE_SCOPE_ITEM_SORTER(new PackageScope()),
IMPORT_DECL_CTX_ITEM_SORTER(new ImportDeclarationContext());
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 487f603

Please sign in to comment.