Skip to content
This repository has been archived by the owner on May 4, 2023. It is now read-only.

Commit

Permalink
- File Listener change
Browse files Browse the repository at this point in the history
- Update documentation
  • Loading branch information
juli1 committed Mar 24, 2022
1 parent ab01f53 commit f000467
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 27 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@

### Security

## [1.4.0]

### Added

* Change the Coding Assistant UI
* Code Snippets are now being cached, improving user experience and speed.

## [1.3.2]

### Added
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pluginGroup = codiga.io.plugins
pluginName = codiga-jetbrains-plugin
pluginVersion = 1.3.2
pluginVersion = 1.4.0


pluginSinceBuild = 212.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.codiga.plugins.jetbrains.assistant.user_variables;

public class UserVariablesConstants {

public static final String REGEXP = "&\\[USER_INPUT:([0-9]+)(:[a-zA-Z0-9\\.\\-\\_]+)?\\]";
//public static final String REGEXP = "USER";
// Constant to catch the user variables.
public static final String REGEXP = "&\\[USER_INPUT:([0-9]+)(:[^\\]]+)?\\]";
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,27 @@
import static io.codiga.plugins.jetbrains.actions.ActionUtils.getLanguageFromEditorForVirtualFile;
import static io.codiga.plugins.jetbrains.actions.ActionUtils.getUnitRelativeFilenamePathFromEditorForVirtualFile;

/**
* This is a listener used to refresh the cache of snippets each time a new file
* is being opened.
*/
public class CacheRefreshEditorListener implements FileEditorManagerListener {

@Override
public void selectionChanged(@NotNull FileEditorManagerEvent event) {
FileEditor fileEditor = event.getNewEditor();

/**
* If we close the editor and we are not openning a new file,
* newFile is null.
*/
if (fileEditor == null) {
return;
}

if (fileEditor.getFile() == null) {
return;
}
Project project = event.getManager().getProject();
String filename = getUnitRelativeFilenamePathFromEditorForVirtualFile(project, fileEditor.getFile());
java.util.List<String> dependencies = DependencyManagement.getInstance().getDependencies(project, fileEditor.getFile()).stream().map(v -> v.getName()).collect(Collectors.toList());
Expand Down
27 changes: 18 additions & 9 deletions src/main/java/io/codiga/plugins/jetbrains/cache/ShortcutCache.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.codiga.plugins.jetbrains.cache;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
Expand All @@ -14,12 +13,18 @@

import static io.codiga.plugins.jetbrains.Constants.LOGGER_NAME;


/**
* This is the shortcut cache that contains all the recipes that have
* a shortcut. It stores all the recipes for all the files being
* opened in the editor.
*/
public final class ShortcutCache {
// Uses a concurrent hashmap to avoid threading issues.
ConcurrentHashMap<ShortcutCacheKey, ShortcutCacheValue> cache;

public static final Logger LOGGER = Logger.getInstance(LOGGER_NAME);
private final CodigaApi codigaApi = ApplicationManager.getApplication().getService(CodigaApi.class);

private static ShortcutCache _INSTANCE = new ShortcutCache();

private ShortcutCache() {
Expand All @@ -31,14 +36,8 @@ public static ShortcutCache getInstance() {
}


@VisibleForTesting
public ConcurrentHashMap<ShortcutCacheKey, ShortcutCacheValue> getCache() {
return this.cache;
}


/**
* Update values for a key
* Update values for a key by pulling the API.
* - check the time on the server for the latest update
* - if there is already a cache value,
* - update only if the timestamp from the server is different
Expand Down Expand Up @@ -75,6 +74,10 @@ private void updateKey(ShortcutCacheKey shortcutCacheKey) {
}
}

/**
* Refresh a cache key in the cache if and only if it needs to be updated.
* @param shortcutCacheKey
*/
public void refreshCacheKey(final ShortcutCacheKey shortcutCacheKey) {
try {
if (cache.containsKey(shortcutCacheKey)) {
Expand All @@ -91,6 +94,12 @@ public void refreshCacheKey(final ShortcutCacheKey shortcutCacheKey) {
}
}

/**
* Just query the cache and gets the value that it contains.
* This method never queries the API.
* @param shortcutCacheKey - the key we are querying
* @return - the list of recipes if any
*/
public List<GetRecipesForClientByShortcutQuery.GetRecipesForClientByShortcut> getRecipesShortcut(ShortcutCacheKey shortcutCacheKey) {
try{
if (cache.containsKey(shortcutCacheKey)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
import java.util.Objects;


/**
* Shortcut for a cache key that is composed of
* - language
* - filename path relative to the project AND UNIX style
* - list of dependencies being used.
*/
public class ShortcutCacheKey {
private final LanguageEnumeration language;
private final String filename;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@

import java.util.List;


/**
* The cache value contains the list of recipes but also
* - the last timestamp on the server for all recipes for the associated key
* if the timestamp on the server did not change, we do not update
* the list of recipes
* - last time we updated the list - we only update periodically and not every time
* we attempt to refresh the cache.
*/
public class ShortcutCacheValue {
private final Long lastTimestampFromServer;
private Long lastUpdateTimestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,15 @@ protected void addCompletions(@NotNull CompletionParameters parameters,
// clean tab indentation to correctly look for completion results
currentLine = currentLine.replace("\t", "");


int column = editor.getCaretModel().getCurrentCaret().getCaretModel().getVisualPosition().getColumn();

// we are not attempting to always autocomplete, especially if the user is attempting to invoke a method
if(!shouldAutocomplete(currentLine, column - 1)) {
return;
}

// Attempt to get the keyword and if not present, just exit.
Optional<String> keyword = getKeywordFromLine(currentLine, column - 1);


if(!keyword.isPresent()) {
return;
}
Expand Down Expand Up @@ -122,10 +121,6 @@ protected void addCompletions(@NotNull CompletionParameters parameters,
List<GetRecipesForClientByShortcutQuery.GetRecipesForClientByShortcut> recipes = ShortcutCache.getInstance().getRecipesShortcut(new ShortcutCacheKey(language, filename, dependenciesName));


/*
* We take only the top three recipes (they come ranked in order from the API).
* For each of them, add a completion item and add a routine to insert the code.
*/
for (GetRecipesForClientByShortcutQuery.GetRecipesForClientByShortcut recipe : recipes) {
if(keyword.isPresent() && ! recipe.shortcut().startsWith(keyword.get())) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,6 @@ public static boolean shouldAutocomplete(String line, int position) {
int pos = position;
boolean spaceMet = false;

if (line.charAt(position) != '/' && line.charAt(position) != '.') {
return false;
}

while(pos > 0){
char c = line.charAt(pos);
pos = pos - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public static void addRecipeInEditor(@NotNull Editor editor,
final String currentCode = document.getText();
final Project project = editor.getProject();

if (project == null) {
return;
}

if (removeCurrentLine) {
// remove the code on the line
int startOffsetToRemove = editor.getCaretModel().getVisualLineStart();
Expand All @@ -65,7 +69,8 @@ public static void addRecipeInEditor(@NotNull Editor editor,
final CodingAssistantCodigaTransform codingAssistantCodigaTransform = new CodingAssistantCodigaTransform(CodigaTransformationContext);
String code = codingAssistantCodigaTransform.findAndTransformVariables(unprocessedCode);

Template template = TemplateManagerImpl.getInstance(project).createTemplate(recipeName, recipeName, code);
// For mysterious reasons, we need to add a newline to the code to not take the next line.
Template template = TemplateManagerImpl.getInstance(project).createTemplate(recipeName, recipeName, code + "\n");
template.setToIndent(true);
template.setToReformat(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ public void testVariableDetection() {
assertEquals(UserVariables.getInstance().getVariablesFromCode(code).get(0).getDefaultValueString(), "blabla");
}


@Test
public void testVariableDetectionMoreCharacters() {
String code = "public class &[USER_INPUT:42:./\\] blibli";
assertEquals(UserVariables.getInstance().getVariablesFromCode(code).size(), 1);
assertEquals(UserVariables.getInstance().getVariablesFromCode(code).get(0).getName(), "42");
assertEquals(UserVariables.getInstance().getVariablesFromCode(code).get(0).getDefaultValueString(), "./\\");
}

@Test
public void testVariableDetectionInvalidPosition() {
String code = "public class &[USER_INPUT:abc:foobar] blibli";
assertEquals(UserVariables.getInstance().getVariablesFromCode(code).size(), 0);
}

@Test
public void testVariableDetectionWithMultipleVariables() {
String code = "public class &[USER_INPUT:42:blabla] blibli \n \n &[USER_INPUT:51:blo] \n &[USER_INPUT:0]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void testGetKeywordFromLine() {
public void testShouldAutocomplete() {
assertEquals(CodePositionUtils.shouldAutocomplete(".", 0), true);
assertEquals(CodePositionUtils.shouldAutocomplete(" blo.", 11), true);
assertEquals(CodePositionUtils.shouldAutocomplete(" blo", 10), false);
assertEquals(CodePositionUtils.shouldAutocomplete(" blo", 10), true);
assertEquals(CodePositionUtils.shouldAutocomplete(" ) blo.", 11), false);
assertEquals(CodePositionUtils.shouldAutocomplete(" b blo.", 11), false);
assertEquals(CodePositionUtils.shouldAutocomplete(" .", 6), true);
Expand Down

0 comments on commit f000467

Please sign in to comment.