Skip to content

Commit

Permalink
Merge pull request #482 from turkeylurkey/extractMessages
Browse files Browse the repository at this point in the history
Extract messages
  • Loading branch information
turkeylurkey authored Jul 25, 2023
2 parents 1ce8d54 + f93571a commit 465408d
Show file tree
Hide file tree
Showing 49 changed files with 491 additions and 366 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2016 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM 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 @@ -14,8 +14,6 @@
*******************************************************************************/
package org.eclipse.lsp4jakarta.jdt.codeAction.proposal;

import java.text.MessageFormat;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
Expand All @@ -37,8 +35,6 @@

public class ExtendClassProposal extends ChangeCorrectionProposal {

private static final String TITLE_MESSAGE = "Let ''{0}'' extend ''{1}''";

private IBinding fBinding;
private CompilationUnit fAstRoot;
private String interfaceType;
Expand All @@ -52,12 +48,6 @@ public ExtendClassProposal(String name, ICompilationUnit targetCU, ITypeBinding
fBinding = binding;
fAstRoot = astRoot;
this.interfaceType = interfaceType;

String[] args = { BasicElementLabels.getJavaElementName(binding.getName()),
BasicElementLabels.getJavaElementName(interfaceType) };

setDisplayName(MessageFormat.format(TITLE_MESSAGE, args));

}

protected ASTRewrite getRewrite() throws CoreException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2016 IBM Corporation and others.
* Copyright (c) 2000, 2023 IBM 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 @@ -14,8 +14,6 @@
*******************************************************************************/
package org.eclipse.lsp4jakarta.jdt.codeAction.proposal;

import java.text.MessageFormat;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.core.ICompilationUnit;
Expand Down Expand Up @@ -44,8 +42,6 @@
*/
public class ImplementInterfaceProposal extends ChangeCorrectionProposal {

private static final String TITLE_MESSAGE = "Let ''{0}'' implement ''{1}''";

private IBinding fBinding;
private CompilationUnit fAstRoot;
private String interfaceType;
Expand All @@ -59,12 +55,6 @@ public ImplementInterfaceProposal(String name, ICompilationUnit targetCU, ITypeB
fBinding = binding;
fAstRoot = astRoot;
this.interfaceType = interfaceType;

String[] args = { BasicElementLabels.getJavaElementName(binding.getName()),
BasicElementLabels.getJavaElementName(interfaceType) };

setDisplayName(MessageFormat.format(TITLE_MESSAGE, args));

}

protected ASTRewrite getRewrite() throws CoreException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2022 IBM Corporation and others.
* Copyright (c) 2022, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -12,6 +12,8 @@
*******************************************************************************/
package org.eclipse.lsp4jakarta.jdt.codeAction.proposal.quickfix;

import org.eclipse.lsp4jakarta.jdt.core.Messages;

/**
* Quickfix for adding attributes to existing annotations
*
Expand All @@ -29,9 +31,6 @@ public InsertAnnotationAttributesQuickFix(String annotation, boolean generateOnl

@Override
protected String getLabel(String annotation, String... attributes) {
String type = "";
String name = "Add " + attributes[0] + " to " + annotation;
name = name.concat(type);
return name;
return Messages.getMessage("AddAtoB", attributes[0], annotation);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* Copyright (c) 2020, 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -29,6 +29,7 @@
import org.eclipse.lsp4jakarta.jdt.codeAction.JavaCodeActionContext;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.ChangeCorrectionProposal;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.NewAnnotationProposal;
import org.eclipse.lsp4jakarta.jdt.core.Messages;

/**
* QuickFix for inserting annotations.
Expand Down Expand Up @@ -123,17 +124,17 @@ protected static void insertAnnotation(Diagnostic diagnostic, JavaCodeActionCont
}

private static String getLabel(String[] annotations) {
StringBuilder name = new StringBuilder("Insert ");
StringBuilder list = new StringBuilder();
for (int i = 0; i < annotations.length; i++) {
String annotation = annotations[i];
String annotationName = annotation.substring(annotation.lastIndexOf('.') + 1, annotation.length());
if (i > 0) {
name.append(", ");
list.append(", "); // assume comma list is ok: @A, @B, @C
}
name.append("@");
name.append(annotationName);
list.append("@"); // Java syntax
list.append(annotationName);
}
return name.toString();
return Messages.getMessage("InsertItem", list.toString());
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* Copyright (c) 2021, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -31,6 +31,7 @@
import org.eclipse.lsp4jakarta.jdt.codeAction.JavaCodeActionContext;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.ChangeCorrectionProposal;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.ModifyAnnotationProposal;
import org.eclipse.lsp4jakarta.jdt.core.Messages;

/**
* Quickfix for adding new annotations with or without attributes
Expand Down Expand Up @@ -122,10 +123,7 @@ protected IBinding getBinding(ASTNode node) {
}

protected String getLabel(String annotation, String... attributes) {
StringBuilder name = new StringBuilder("Insert ");
name.append("@");
name.append(annotation);
return name.toString();
return Messages.getMessage("InsertItem", "@" + annotation); // uses Java syntax
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2020 Red Hat Inc. and others.
* Copyright (c) 2020, 2023 Red Hat Inc. and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -28,6 +28,7 @@
import org.eclipse.lsp4jakarta.jdt.codeAction.JavaCodeActionContext;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.ChangeCorrectionProposal;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.DeleteAnnotationProposal;
import org.eclipse.lsp4jakarta.jdt.core.Messages;

/**
* QuickFix for removing annotations. Modified from
Expand Down Expand Up @@ -118,16 +119,16 @@ protected String[] getAnnotations() {
}

private static String getLabel(String[] annotations) {
StringBuilder name = new StringBuilder("Remove ");
StringBuilder list = new StringBuilder();
for (int i = 0; i < annotations.length; i++) {
String annotation = annotations[i];
String annotationName = annotation.substring(annotation.lastIndexOf('.') + 1, annotation.length());
if (i > 0) {
name.append(", ");
list.append(", "); // assume comma list is ok: @A, @B, @C
}
name.append("@");
name.append(annotationName);
list.append("@"); // Java syntax
list.append(annotationName);
}
return name.toString();
return Messages.getMessage("RemoveItem", list.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* Copyright (c) 2021, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -33,6 +33,7 @@
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.ChangeCorrectionProposal;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.ModifyModifiersProposal;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.RemoveParamsProposal;
import org.eclipse.lsp4jakarta.jdt.core.Messages;
import org.eclipse.lsp4jakarta.jdt.core.annotations.AnnotationConstants;
import org.eclipse.lsp4jakarta.jdt.core.beanvalidation.BeanValidationConstants;

Expand All @@ -50,7 +51,7 @@ public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context,
IMethodBinding parentMethod = parentNode.resolveBinding();
List<CodeAction> codeActions = new ArrayList<>();
List<SingleVariableDeclaration> parameters = (List<SingleVariableDeclaration>) parentNode.parameters();
String name = "Remove all parameters";
String name = Messages.getMessage("RemoveAllParameters");
ChangeCorrectionProposal proposal = new RemoveParamsProposal(name, context.getCompilationUnit(),
context.getASTRoot(), parentMethod, 0, parameters, null);
CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* Copyright (c) 2021, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -30,6 +30,7 @@
import org.eclipse.lsp4jakarta.jdt.codeAction.IJavaCodeActionParticipant;
import org.eclipse.lsp4jakarta.jdt.codeAction.JavaCodeActionContext;
import org.eclipse.lsp4jakarta.jdt.codeAction.proposal.ModifyModifiersProposal;
import org.eclipse.lsp4jakarta.jdt.core.Messages;


/**
Expand Down Expand Up @@ -104,21 +105,9 @@ private void removeModifier(Diagnostic diagnostic, JavaCodeActionContext context
// Remove the modifier and the proper import by using JDT Core Manipulation
// API
ASTNode coveredNode = context.getCoveredNode().getParent();
String type = "";
if (diagnostic.getData().toString().equals(String.valueOf(IJavaElement.LOCAL_VARIABLE))){
type = "variable";
} else if (diagnostic.getData().toString().equals(String.valueOf(IJavaElement.FIELD))) {
type = "field";
} else if (diagnostic.getData().toString().equals(String.valueOf(IJavaElement.METHOD))) {
type = "method";
} else if (diagnostic.getData().toString().equals(String.valueOf(IJavaElement.CLASS_FILE)) ||
diagnostic.getData().toString().equals(String.valueOf(IJavaElement.TYPE))) {
type = "class";
}
String label = getLabel(diagnostic, modifier);

String name = "Remove the \'" + modifier[0] + "\' modifier from this ";
name = name.concat(type);
ModifyModifiersProposal proposal = new ModifyModifiersProposal(name, context.getCompilationUnit(),
ModifyModifiersProposal proposal = new ModifyModifiersProposal(label, context.getCompilationUnit(),
context.getASTRoot(), parentType, 0, coveredNode, new ArrayList<>(), Arrays.asList(modifier));
CodeAction codeAction = context.convertToCodeAction(proposal, diagnostic);

Expand All @@ -127,6 +116,23 @@ private void removeModifier(Diagnostic diagnostic, JavaCodeActionContext context
}
}

private String getLabel(Diagnostic diagnostic, String... modifier) {
String label;
if (diagnostic.getData().toString().equals(String.valueOf(IJavaElement.LOCAL_VARIABLE))){
label = Messages.getMessage("RemoveTheModifierFromThisVariable", modifier[0]);
} else if (diagnostic.getData().toString().equals(String.valueOf(IJavaElement.FIELD))) {
label = Messages.getMessage("RemoveTheModifierFromThisField", modifier[0]);
} else if (diagnostic.getData().toString().equals(String.valueOf(IJavaElement.METHOD))) {
label = Messages.getMessage("RemoveTheModifierFromThisMethod", modifier[0]);
} else if (diagnostic.getData().toString().equals(String.valueOf(IJavaElement.CLASS_FILE)) ||
diagnostic.getData().toString().equals(String.valueOf(IJavaElement.TYPE))) {
label = Messages.getMessage("RemoveTheModifierFromThisClass", modifier[0]);
} else {
label = Messages.getMessage("RemoveTheModifierFromThis", modifier[0], "");
}
return label;
}

/**
* Removes a set of modifiers from a given ASTNode with a given code action label
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021 IBM Corporation and others.
* Copyright (c) 2021, 2023 IBM Corporation and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -27,6 +27,7 @@
import org.eclipse.lsp4j.CodeAction;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4jakarta.jdt.codeAction.JavaCodeActionContext;
import org.eclipse.lsp4jakarta.jdt.core.Messages;

/**
* QuickFix for removing parameter annotations
Expand Down Expand Up @@ -64,14 +65,15 @@ public List<? extends CodeAction> getCodeActions(JavaCodeActionContext context,
}
}

StringBuilder sb = new StringBuilder("Remove the ");
StringBuilder sb = new StringBuilder();
// Java annotations in comma delimited list, assume that is ok.
sb.append("'@").append(annotationsToRemove.get(0)).append("'");
for (int i = 1; i < annotationsToRemove.size();i++) {
sb.append(", '@").append(annotationsToRemove.get(i)).append("'");
}
sb.append(" modifier from parameter '").append(parameter.getName().toString()).append("'");
String label = Messages.getMessage("RemoveTheModifierFromParameter", sb.toString(), parameter.getName().toString());

removeModifier(diagnostic, context, parentMethod, codeActions, parameter, sb.toString(),
removeModifier(diagnostic, context, parentMethod, codeActions, parameter, label,
(String []) annotationsToRemove.toArray(new String[annotationsToRemove.size()]));
}

Expand Down
Loading

0 comments on commit 465408d

Please sign in to comment.