-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into label-annotation
- Loading branch information
Showing
22 changed files
with
1,429 additions
and
175 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
57 changes: 57 additions & 0 deletions
57
rosetta-ide/src/main/java/com/regnosys/rosetta/ide/quickfix/AbstractCodeActionProvider.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,57 @@ | ||
package com.regnosys.rosetta.ide.quickfix; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.TextEdit; | ||
import org.eclipse.lsp4j.WorkspaceEdit; | ||
import org.eclipse.xtext.ide.server.ILanguageServerAccess; | ||
import org.eclipse.xtext.ide.server.TextEditAcceptor; | ||
import org.eclipse.xtext.ide.server.codeActions.ICodeActionService2.Options; | ||
|
||
import com.regnosys.rosetta.rosetta.RosettaModel; | ||
|
||
public abstract class AbstractCodeActionProvider implements ICodeActionProvider { | ||
|
||
private static final Logger LOG = Logger.getLogger(RosettaCodeActionProvider.class); | ||
|
||
@Override | ||
public CodeAction resolve(CodeAction unresolved, Options options) { | ||
String title = unresolved.getTitle(); | ||
if (unresolved == null || title == null) { | ||
return null; | ||
} | ||
|
||
Method resolutionMethod = findResolutionMethod(getClass(), title); | ||
|
||
if (resolutionMethod != null) { | ||
try { | ||
RosettaModel model = (RosettaModel) options.getResource().getContents().get(0); | ||
List<TextEdit> edits = (List<TextEdit>) resolutionMethod.invoke(this, model); | ||
|
||
ILanguageServerAccess languageServerAccess = options.getLanguageServerAccess(); | ||
|
||
WorkspaceEdit workspaceEdit = new WorkspaceEdit(); | ||
TextEditAcceptor editAcceptor = new TextEditAcceptor(workspaceEdit, languageServerAccess); | ||
String uri = options.getResource().getURI().toString(); | ||
|
||
editAcceptor.accept(uri, options.getDocument(), edits); | ||
unresolved.setEdit(workspaceEdit); | ||
return unresolved; | ||
} catch (Exception e) { | ||
LOG.error("Error resolving code action: " + title, e); | ||
} | ||
} | ||
return unresolved; | ||
} | ||
|
||
private Method findResolutionMethod(Class<? extends AbstractCodeActionProvider> clazz, String title) { | ||
return Arrays.stream(clazz.getMethods()).filter(method -> { | ||
CodeActionResolution annotation = method.getAnnotation(CodeActionResolution.class); | ||
return annotation != null && annotation.value().equals(title); | ||
}).findFirst().orElse(null); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
rosetta-ide/src/main/java/com/regnosys/rosetta/ide/quickfix/CodeActionResolution.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,17 @@ | ||
package com.regnosys.rosetta.ide.quickfix; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ ElementType.METHOD }) | ||
public @interface CodeActionResolution { | ||
/** | ||
* Specifies the title of the code action for which the annotated method provides a resolution. | ||
* | ||
* @return the title of the associated code action. | ||
*/ | ||
String value(); | ||
} |
12 changes: 12 additions & 0 deletions
12
rosetta-ide/src/main/java/com/regnosys/rosetta/ide/quickfix/ICodeActionProvider.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,12 @@ | ||
package com.regnosys.rosetta.ide.quickfix; | ||
|
||
import java.util.List; | ||
|
||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.xtext.ide.server.codeActions.ICodeActionService2.Options; | ||
|
||
public interface ICodeActionProvider { | ||
List<CodeAction> getCodeActions(Options options); | ||
|
||
CodeAction resolve(CodeAction unresolved, Options options); | ||
} |
8 changes: 8 additions & 0 deletions
8
rosetta-ide/src/main/java/com/regnosys/rosetta/ide/quickfix/IResolveCodeActionService.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,8 @@ | ||
package com.regnosys.rosetta.ide.quickfix; | ||
|
||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.xtext.ide.server.codeActions.ICodeActionService2.Options; | ||
|
||
public interface IResolveCodeActionService { | ||
CodeAction getCodeActionResolution(CodeAction unresolved, Options baseOptions); | ||
} |
51 changes: 51 additions & 0 deletions
51
rosetta-ide/src/main/java/com/regnosys/rosetta/ide/quickfix/RosettaCodeActionProvider.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,51 @@ | ||
package com.regnosys.rosetta.ide.quickfix; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.eclipse.emf.common.util.EList; | ||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.CodeActionKind; | ||
import org.eclipse.lsp4j.Range; | ||
import org.eclipse.lsp4j.TextEdit; | ||
import org.eclipse.xtext.ide.server.codeActions.ICodeActionService2.Options; | ||
|
||
import com.regnosys.rosetta.ide.util.CodeActionUtils; | ||
import com.regnosys.rosetta.rosetta.Import; | ||
import com.regnosys.rosetta.rosetta.RosettaModel; | ||
import com.regnosys.rosetta.utils.ImportManagementService; | ||
|
||
public class RosettaCodeActionProvider extends AbstractCodeActionProvider { | ||
@Inject | ||
private ImportManagementService importManagementService; | ||
@Inject | ||
private CodeActionUtils codeActionUtils; | ||
|
||
@Override | ||
public List<CodeAction> getCodeActions(Options options) { | ||
List<CodeAction> result = new ArrayList<>(); | ||
|
||
// Handle Sorting CodeAction | ||
RosettaModel model = (RosettaModel) options.getResource().getContents().get(0); | ||
if (!importManagementService.isSorted(model.getImports())) { | ||
result.add(codeActionUtils.createUnresolvedCodeAction("Sort imports", options.getCodeActionParams(), | ||
CodeActionKind.SourceOrganizeImports)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@CodeActionResolution("Sort imports") | ||
public List<TextEdit> sortImports(RosettaModel model) { | ||
EList<Import> imports = model.getImports(); | ||
|
||
Range importsRange = codeActionUtils.getImportsRange(imports); | ||
|
||
importManagementService.sortImports(imports); | ||
String sortedImportsText = importManagementService.toString(imports); | ||
|
||
return List.of(new TextEdit(importsRange, sortedImportsText)); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
rosetta-ide/src/main/java/com/regnosys/rosetta/ide/quickfix/RosettaCodeActionService.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,78 @@ | ||
/* | ||
* Copyright 2024 REGnosys | ||
* | ||
* Licensed 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 com.regnosys.rosetta.ide.quickfix; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.eclipse.lsp4j.CodeAction; | ||
import org.eclipse.lsp4j.CodeActionKind; | ||
import org.eclipse.lsp4j.Command; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.eclipse.lsp4j.jsonrpc.messages.Either; | ||
import org.eclipse.xtext.ide.editor.quickfix.DiagnosticResolution; | ||
import org.eclipse.xtext.ide.editor.quickfix.IQuickFixProvider; | ||
import org.eclipse.xtext.ide.server.codeActions.ICodeActionService2; | ||
|
||
import com.regnosys.rosetta.ide.util.CodeActionUtils; | ||
|
||
public class RosettaCodeActionService implements ICodeActionService2 { | ||
|
||
@Inject | ||
private IQuickFixProvider quickfixes; | ||
@Inject | ||
private CodeActionUtils codeActionUtils; | ||
@Inject | ||
private ICodeActionProvider codeActionProvider; | ||
|
||
@Override | ||
public List<Either<Command, CodeAction>> getCodeActions(Options options) { | ||
List<Either<Command, CodeAction>> result = new ArrayList<>(); | ||
|
||
//Handle Code Actions | ||
List<Either<Command, CodeAction>> codeActions = codeActionProvider.getCodeActions(options).stream() | ||
.map(action -> Either.<Command, CodeAction>forRight(action)) | ||
.collect(Collectors.toList()); | ||
result.addAll(codeActions); | ||
|
||
boolean handleQuickfixes = options.getCodeActionParams().getContext().getOnly() == null | ||
|| options.getCodeActionParams().getContext().getOnly().isEmpty() | ||
|| options.getCodeActionParams().getContext().getOnly().contains(CodeActionKind.QuickFix); | ||
|
||
if (handleQuickfixes) { | ||
List<Diagnostic> diagnostics = options.getCodeActionParams().getContext().getDiagnostics(); | ||
|
||
for (Diagnostic diagnostic : diagnostics) { | ||
Options diagnosticOptions = codeActionUtils.createOptionsForSingleDiagnostic(options, diagnostic); | ||
List<DiagnosticResolution> resolutions = quickfixes.getResolutions(diagnosticOptions, diagnostic) | ||
.stream().sorted(Comparator.nullsLast(Comparator.comparing(DiagnosticResolution::getLabel))) | ||
.collect(Collectors.toList()); | ||
for (DiagnosticResolution resolution : resolutions) { | ||
result.add(Either | ||
.forRight(codeActionUtils.createUnresolvedFix(resolution.getLabel(), options.getCodeActionParams(), diagnostic))); | ||
} | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
Oops, something went wrong.