Skip to content

Commit

Permalink
Use ASTProvider to getAST for source action handlers (#1533)
Browse files Browse the repository at this point in the history
Signed-off-by: Jinbo Wang <[email protected]>
  • Loading branch information
testforstephen authored Aug 27, 2020
1 parent 86fb065 commit 45363e4
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 114 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Microsoft Corporation and others.
* Copyright (c) 2019-2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -22,6 +22,8 @@
import java.util.stream.Stream;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
Expand All @@ -38,30 +40,36 @@
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.core.dom.rewrite.ListRewrite;
import org.eclipse.jdt.internal.core.manipulation.StubUtility;
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2Core;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.TextEdit;

public class OverrideMethodsOperation {

// For test purpose
public static List<OverridableMethod> listOverridableMethods(IType type) {
return listOverridableMethods(type, new NullProgressMonitor());
}

public static List<OverridableMethod> listOverridableMethods(IType type, IProgressMonitor monitor) {
if (type == null || type.getCompilationUnit() == null) {
return Collections.emptyList();
}

List<OverridableMethod> overridables = new ArrayList<>();
RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL);
CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor);
if (astRoot == null) {
return Collections.emptyList();
}

try {
ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type);
if (typeBinding == null) {
Expand Down Expand Up @@ -89,13 +97,21 @@ public static List<OverridableMethod> listOverridableMethods(IType type) {
return overridables;
}

// For test purpose
public static TextEdit addOverridableMethods(IType type, OverridableMethod[] overridableMethods) {
return addOverridableMethods(type, overridableMethods, new NullProgressMonitor());
}

public static TextEdit addOverridableMethods(IType type, OverridableMethod[] overridableMethods, IProgressMonitor monitor) {
if (type == null || type.getCompilationUnit() == null || overridableMethods == null || overridableMethods.length == 0) {
return null;
}

RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL);
CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor);
if (astRoot == null) {
return null;
}

try {
ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type);
if (typeBinding == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017-2019 Red Hat Inc. and others.
* Copyright (c) 2017-2020 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -84,14 +84,20 @@ public List<Either<Command, CodeAction>> getCodeActionCommands(CodeActionParams
if (unit == null || monitor.isCanceled()) {
return Collections.emptyList();
}

CompilationUnit astRoot = getASTRoot(unit, monitor);
if (astRoot == null || monitor.isCanceled()) {
return Collections.emptyList();
}

int start = DiagnosticsHelper.getStartOffset(unit, params.getRange());
int end = DiagnosticsHelper.getEndOffset(unit, params.getRange());
InnovationContext context = new InnovationContext(unit, start, end - start);
context.setASTRoot(getASTRoot(unit));
context.setASTRoot(astRoot);
IProblemLocationCore[] locations = this.getProblemLocationCores(unit, params.getContext().getDiagnostics());
if (monitor.isCanceled()) {
return Collections.emptyList();
}
IProblemLocationCore[] locations = this.getProblemLocationCores(unit, params.getContext().getDiagnostics());

List<String> codeActionKinds = new ArrayList<>();
if (params.getContext().getOnly() != null && !params.getContext().getOnly().isEmpty()) {
Expand Down Expand Up @@ -161,7 +167,7 @@ public List<Either<Command, CodeAction>> getCodeActionCommands(CodeActionParams
return Collections.emptyList();
}
if (containsKind(codeActionKinds, CodeActionKind.Source)) {
codeActions.addAll(sourceAssistProcessor.getSourceActionCommands(params, context, locations));
codeActions.addAll(sourceAssistProcessor.getSourceActionCommands(params, context, locations, monitor));
}
if (monitor.isCanceled()) {
return Collections.emptyList();
Expand Down Expand Up @@ -233,7 +239,11 @@ private static int getProblemId(Diagnostic diagnostic) {
}

public static CompilationUnit getASTRoot(ICompilationUnit unit) {
return CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, new NullProgressMonitor());
return getASTRoot(unit, new NullProgressMonitor());
}

public static CompilationUnit getASTRoot(ICompilationUnit unit, IProgressMonitor monitor) {
return CoreASTProvider.getInstance().getAST(unit, CoreASTProvider.WAIT_YES, monitor);
}

private static class ChangeCorrectionProposalComparator implements Comparator<ChangeCorrectionProposal> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Microsoft Corporation and others.
* Copyright (c) 2019-2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -21,6 +21,8 @@
import java.util.Optional;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IType;
Expand All @@ -33,13 +35,12 @@
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Modifier;
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.internal.corext.codemanipulation.AddCustomConstructorOperation;
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2Core;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.Bindings;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding;
import org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspVariableBinding;
Expand All @@ -51,20 +52,27 @@
import org.eclipse.text.edits.TextEdit;

public class GenerateConstructorsHandler {

// For test purpose
public static CheckConstructorsResponse checkConstructorsStatus(CodeActionParams params) {
IType type = SourceAssistProcessor.getSelectionType(params);
return checkConstructorStatus(type);
return checkConstructorsStatus(params, new NullProgressMonitor());
}

public static CheckConstructorsResponse checkConstructorsStatus(CodeActionParams params, IProgressMonitor monitor) {
IType type = SourceAssistProcessor.getSelectionType(params, monitor);
return checkConstructorStatus(type, monitor);
}

public static CheckConstructorsResponse checkConstructorStatus(IType type) {
public static CheckConstructorsResponse checkConstructorStatus(IType type, IProgressMonitor monitor) {
if (type == null || type.getCompilationUnit() == null) {
return new CheckConstructorsResponse();
}

try {
RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL);
CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor);
if (astRoot == null) {
return new CheckConstructorsResponse();
}

ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type);
if (typeBinding == null) {
return new CheckConstructorsResponse();
Expand Down Expand Up @@ -123,27 +131,35 @@ private static IMethodBinding[] getVisibleConstructors(CompilationUnit astRoot,
}
}

public static WorkspaceEdit generateConstructors(GenerateConstructorsParams params) {
IType type = SourceAssistProcessor.getSelectionType(params.context);
TextEdit edit = generateConstructors(type, params.constructors, params.fields);
public static WorkspaceEdit generateConstructors(GenerateConstructorsParams params, IProgressMonitor monitor) {
IType type = SourceAssistProcessor.getSelectionType(params.context, monitor);
TextEdit edit = generateConstructors(type, params.constructors, params.fields, monitor);
return (edit == null) ? null : SourceAssistProcessor.convertToWorkspaceEdit(type.getCompilationUnit(), edit);
}

public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields) {
public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields, IProgressMonitor monitor) {
Preferences preferences = JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
CodeGenerationSettings settings = new CodeGenerationSettings();
settings.createComments = preferences.isCodeGenerationTemplateGenerateComments();
return generateConstructors(type, constructors, fields, settings);
return generateConstructors(type, constructors, fields, settings, monitor);
}

// For test purpose
public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields, CodeGenerationSettings settings) {
return generateConstructors(type, constructors, fields, settings, new NullProgressMonitor());
}

public static TextEdit generateConstructors(IType type, LspMethodBinding[] constructors, LspVariableBinding[] fields, CodeGenerationSettings settings, IProgressMonitor monitor) {
if (type == null || type.getCompilationUnit() == null || constructors == null || constructors.length == 0) {
return null;
}

try {
RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL);
CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor);
if (astRoot == null) {
return null;
}

ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type);
if (typeBinding != null) {
Map<String, IVariableBinding> fieldBindings = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Microsoft Corporation and others.
* Copyright (c) 2019-2020 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -21,6 +21,8 @@
import java.util.Map;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
Expand All @@ -29,13 +31,12 @@
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.manipulation.CoreASTProvider;
import org.eclipse.jdt.internal.corext.codemanipulation.AddDelegateMethodsOperation;
import org.eclipse.jdt.internal.corext.codemanipulation.AddDelegateMethodsOperation.DelegateEntry;
import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility2Core;
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
import org.eclipse.jdt.internal.corext.dom.IASTSharedValues;
import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
import org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin;
import org.eclipse.jdt.ls.core.internal.corext.util.JdtFlags;
import org.eclipse.jdt.ls.core.internal.handlers.JdtDomModels.LspMethodBinding;
Expand Down Expand Up @@ -73,15 +74,23 @@ private static boolean isArray(IField field) throws JavaModelException {
return Signature.getArrayCount(field.getTypeSignature()) > 0;
}

// For test purpose.
public static CheckDelegateMethodsResponse checkDelegateMethodsStatus(CodeActionParams params) {
IType type = SourceAssistProcessor.getSelectionType(params);
return checkDelegateMethodsStatus(params, new NullProgressMonitor());
}

public static CheckDelegateMethodsResponse checkDelegateMethodsStatus(CodeActionParams params, IProgressMonitor monitor) {
IType type = SourceAssistProcessor.getSelectionType(params, monitor);
if (type == null || type.getCompilationUnit() == null) {
return new CheckDelegateMethodsResponse();
}

try {
RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL);
CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor);
if (astRoot == null) {
return new CheckDelegateMethodsResponse();
}

ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type);
if (typeBinding == null) {
return new CheckDelegateMethodsResponse();
Expand All @@ -107,23 +116,31 @@ public static CheckDelegateMethodsResponse checkDelegateMethodsStatus(CodeAction
return new CheckDelegateMethodsResponse();
}

public static WorkspaceEdit generateDelegateMethods(GenerateDelegateMethodsParams params) {
IType type = SourceAssistProcessor.getSelectionType(params.context);
public static WorkspaceEdit generateDelegateMethods(GenerateDelegateMethodsParams params, IProgressMonitor monitor) {
IType type = SourceAssistProcessor.getSelectionType(params.context, monitor);
Preferences preferences = JavaLanguageServerPlugin.getPreferencesManager().getPreferences();
CodeGenerationSettings settings = new CodeGenerationSettings();
settings.createComments = preferences.isCodeGenerationTemplateGenerateComments();
TextEdit edit = generateDelegateMethods(type, params.delegateEntries, settings);
TextEdit edit = generateDelegateMethods(type, params.delegateEntries, settings, monitor);
return (edit == null) ? null : SourceAssistProcessor.convertToWorkspaceEdit(type.getCompilationUnit(), edit);
}

// For test purpose.
public static TextEdit generateDelegateMethods(IType type, LspDelegateEntry[] delegateEntries, CodeGenerationSettings settings) {
return generateDelegateMethods(type, delegateEntries, settings, new NullProgressMonitor());
}

public static TextEdit generateDelegateMethods(IType type, LspDelegateEntry[] delegateEntries, CodeGenerationSettings settings, IProgressMonitor monitor) {
if (type == null || type.getCompilationUnit() == null || delegateEntries == null || delegateEntries.length == 0) {
return null;
}

try {
RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL);
CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true);
CompilationUnit astRoot = CoreASTProvider.getInstance().getAST(type.getCompilationUnit(), CoreASTProvider.WAIT_YES, monitor);
if (astRoot == null) {
return null;
}

ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type);
if (typeBinding == null) {
return null;
Expand Down
Loading

0 comments on commit 45363e4

Please sign in to comment.