-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement itemDefaults for completionList
Signed-off-by: Jessica He <[email protected]>
- Loading branch information
1 parent
2b0fe29
commit 3bdf51c
Showing
16 changed files
with
596 additions
and
20 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
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
115 changes: 115 additions & 0 deletions
115
org.eclipse.lemminx/src/main/java/org/eclipse/lemminx/utils/CompletionItemDefaultsUtils.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,115 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.utils; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.eclipse.lemminx.services.CompletionResponse; | ||
import org.eclipse.lemminx.settings.SharedSettings; | ||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.eclipse.lsp4j.CompletionItemDefaults; | ||
import org.eclipse.lsp4j.InsertTextFormat; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
|
||
/** | ||
* CompletionItemDefaultsUtils for implementing itemDefaults in completion list | ||
*/ | ||
public class CompletionItemDefaultsUtils { | ||
|
||
private static final String ITEM_DEFAULTS_EDIT_RANGE = "editRange"; | ||
|
||
private static final String ITEM_DEFAULTS_INSERT_TEXT_FORMAT = "insertTextFormat"; | ||
|
||
public static void process(CompletionResponse completionResponse, SharedSettings sharedSettings) { | ||
CompletionItemDefaults itemDefaults = new CompletionItemDefaults(); | ||
List<CompletionItem> completionList = completionResponse.getItems(); | ||
if (sharedSettings.getCompletionSettings().isCompletionListItemDefaultsSupport(ITEM_DEFAULTS_EDIT_RANGE)) { | ||
Range editRange = findMostCommonEditRange(completionList); | ||
itemDefaults.setEditRange(Either.forLeft(editRange)); | ||
for (CompletionItem item : completionList) { | ||
if (item.getTextEdit().isLeft() && item.getTextEdit().getLeft().getRange().equals(editRange)) { | ||
item.setTextEditText(item.getTextEdit().getLeft().getNewText()); | ||
item.setTextEdit(null); | ||
} | ||
} | ||
completionResponse.setItemDefaults(itemDefaults); | ||
} | ||
if (sharedSettings.getCompletionSettings() | ||
.isCompletionListItemDefaultsSupport(ITEM_DEFAULTS_INSERT_TEXT_FORMAT)) { | ||
itemDefaults.setInsertTextFormat(findMostCommonInsertTextFormat(completionResponse.getItems())); | ||
for (CompletionItem item : completionList) { | ||
if (item.getInsertTextFormat() == itemDefaults.getInsertTextFormat()) { | ||
item.setInsertTextFormat(null); | ||
} | ||
} | ||
completionResponse.setItemDefaults(itemDefaults); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the most common editRange in the completion list. | ||
* | ||
* @param completionList the completion response | ||
* @return the most common editRange in the completion list | ||
*/ | ||
private static Range findMostCommonEditRange(List<CompletionItem> completionList) { | ||
Map<Range, Integer> countMapEditRange = new HashMap<>(); | ||
|
||
for (CompletionItem item : completionList) { | ||
if (item.getTextEdit().getLeft() != null) { | ||
countMapEditRange.put(item.getTextEdit().getLeft().getRange(), | ||
countMapEditRange.getOrDefault(item.getTextEdit().getLeft().getRange(), 0) + 1); | ||
} | ||
} | ||
|
||
Range mostCommonRange = null; | ||
int maxCount = 0; | ||
for (Map.Entry<Range, Integer> entry : countMapEditRange.entrySet()) { | ||
if (entry.getValue() > maxCount) { | ||
mostCommonRange = entry.getKey(); | ||
maxCount = entry.getValue(); | ||
} | ||
} | ||
return mostCommonRange; | ||
} | ||
|
||
/** | ||
* Returns the most common insertTextFormat in the completion list. | ||
* | ||
* @param completionList the completion response | ||
* @return the most common insertTextFormat in the completion list | ||
*/ | ||
private static InsertTextFormat findMostCommonInsertTextFormat(List<CompletionItem> completionList) { | ||
Map<InsertTextFormat, Integer> countMapInsertTextFormat = new HashMap<>(); | ||
|
||
for (CompletionItem item : completionList) { | ||
if (item.getInsertTextFormat() != null) { | ||
countMapInsertTextFormat.put(item.getInsertTextFormat(), | ||
countMapInsertTextFormat.getOrDefault(item.getInsertTextFormat(), 0) + 1); | ||
} | ||
} | ||
|
||
InsertTextFormat mostCommonInsertTextFormat = null; | ||
int maxCount = 0; | ||
for (Map.Entry<InsertTextFormat, Integer> entry : countMapInsertTextFormat.entrySet()) { | ||
if (entry.getValue() > maxCount) { | ||
mostCommonInsertTextFormat = entry.getKey(); | ||
maxCount = entry.getValue(); | ||
} | ||
} | ||
return mostCommonInsertTextFormat; | ||
} | ||
|
||
} |
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.