remove noisy final keywords in code snippets

This commit is contained in:
Karol Lewandowski 2022-03-18 10:56:48 +01:00
parent 4e512d99d3
commit 86c69eec32
4 changed files with 47 additions and 43 deletions

View File

@ -43,19 +43,19 @@ So the code would probably look like this:
```java ```java
XmlFile file = ...; XmlFile file = ...;
final XmlDocument document = file.getDocument(); XmlDocument document = file.getDocument();
if (document != null) { if (document != null) {
final XmlTag rootTag = document.getRootTag(); XmlTag rootTag = document.getRootTag();
if (rootTag != null) { if (rootTag != null) {
final XmlTag foo = rootTag.findFirstSubTag("foo"); XmlTag foo = rootTag.findFirstSubTag("foo");
if (foo != null) { if (foo != null) {
final XmlTag[] bars = foo.findSubTags("bar"); XmlTag[] bars = foo.findSubTags("bar");
if (bars.length > 1) { if (bars.length > 1) {
String s = bars[1].getValue().getTrimmedText(); String s = bars[1].getValue().getTrimmedText();
// do something // do something
} }
}
} }
}
} }
``` ```
@ -565,9 +565,9 @@ For example, if you have two DOM element classes — `Foo` and `Bar` — your vi
```java ```java
class MyVisitor implements DomElementVisitor { class MyVisitor implements DomElementVisitor {
void visitDomElement(DomElement element) {} void visitDomElement(DomElement element) {}
void visitFoo(Foo foo) {} void visitFoo(Foo foo) {}
void visitBar(Bar bar) {} void visitBar(Bar bar) {}
} }
``` ```
@ -582,10 +582,10 @@ You can easily write this helper method and annotate it with the `@PropertyAcces
For example, you can write: For example, you can write:
```java ```java
GenericDomValue<String> getVeryLongName() GenericDomValue<String> getVeryLongName();
@PropertyAccessor("very-long-name") @PropertyAccessor("very-long-name")
GenericDomValue<String> getName() GenericDomValue<String> getName();
``` ```
In this case, the second method will return just the same as the first one. In this case, the second method will return just the same as the first one.

View File

@ -78,7 +78,7 @@ import org.intellij.sdk.language.SimpleFileType;
public class SimpleElementFactory { public class SimpleElementFactory {
public static SimpleProperty createProperty(Project project, String name) { public static SimpleProperty createProperty(Project project, String name) {
final SimpleFile file = createFile(project, name); SimpleFile file = createFile(project, name);
return (SimpleProperty) file.getFirstChild(); return (SimpleProperty) file.getFirstChild();
} }

View File

@ -52,8 +52,8 @@ After ensuring that `Project` and `Editor` objects are available, the `Editor` o
public class EditorHandlerIllustration extends AnAction { public class EditorHandlerIllustration extends AnAction {
@Override @Override
public void update(@NotNull final AnActionEvent e) { public void update(@NotNull final AnActionEvent e) {
final Project project = e.getProject(); Project project = e.getProject();
final Editor editor = e.getData(CommonDataKeys.EDITOR); Editor editor = e.getData(CommonDataKeys.EDITOR);
// Make sure at least one caret is available // Make sure at least one caret is available
boolean menuAllowed = false; boolean menuAllowed = false;
@ -77,9 +77,10 @@ For cloning a caret below the primary caret, the constant is `ACTION_EDITOR_CLON
Based on that constant, the `EditorActionManager` returns an instance of [`CloneCaretActionHandler`](upsource:///platform/platform-impl/src/com/intellij/openapi/editor/actions/CloneCaretActionHandler.java), a subclass of `EditorActionHandler`. Based on that constant, the `EditorActionManager` returns an instance of [`CloneCaretActionHandler`](upsource:///platform/platform-impl/src/com/intellij/openapi/editor/actions/CloneCaretActionHandler.java), a subclass of `EditorActionHandler`.
```java ```java
// Snippet from EditorHandlerIllustration.actionPerformed() // Snippet from EditorHandlerIllustration.actionPerformed()
final EditorActionManager actionManager = EditorActionManager.getInstance(); EditorActionManager actionManager = EditorActionManager.getInstance();
final EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_CLONE_CARET_BELOW); EditorActionHandler actionHandler =
actionManager.getActionHandler(IdeActions.ACTION_EDITOR_CLONE_CARET_BELOW);
``` ```
### Using an EditorActionHandler to Clone the Caret ### Using an EditorActionHandler to Clone the Caret
@ -90,10 +91,12 @@ To clone the caret requires only calling the `EditorActionHandler.execute()` met
public class EditorHandlerIllustration extends AnAction { public class EditorHandlerIllustration extends AnAction {
@Override @Override
public void actionPerformed(@NotNull final AnActionEvent e) { public void actionPerformed(@NotNull final AnActionEvent e) {
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR); Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final EditorActionManager actionManager = EditorActionManager.getInstance(); EditorActionManager actionManager = EditorActionManager.getInstance();
final EditorActionHandler actionHandler = actionManager.getActionHandler(IdeActions.ACTION_EDITOR_CLONE_CARET_BELOW); EditorActionHandler actionHandler =
actionHandler.execute(editor, editor.getCaretModel().getPrimaryCaret(), e.getDataContext()); actionManager.getActionHandler(IdeActions.ACTION_EDITOR_CLONE_CARET_BELOW);
actionHandler.execute(editor,
editor.getCaretModel().getPrimaryCaret(), e.getDataContext());
} }
} }
``` ```
@ -125,7 +128,7 @@ class MyTypedHandler implements TypedActionHandler {
char c, char c,
@NotNull DataContext dataContext) { @NotNull DataContext dataContext) {
final Document document = editor.getDocument(); final Document document = editor.getDocument();
final Project project = editor.getProject(); Project project = editor.getProject();
Runnable runnable = () -> document.insertString(0, "editor_basics\n"); Runnable runnable = () -> document.insertString(0, "editor_basics\n");
WriteCommandAction.runWriteCommandAction(project, runnable); WriteCommandAction.runWriteCommandAction(project, runnable);
} }
@ -142,11 +145,11 @@ The method `TypedAction.setupHandler()` is used to register the custom `MyTypedH
```java ```java
public class EditorHandlerIllustration extends AnAction { public class EditorHandlerIllustration extends AnAction {
static { static {
final EditorActionManager actionManager = EditorActionManager.getInstance(); EditorActionManager actionManager = EditorActionManager.getInstance();
final TypedAction typedAction = actionManager.getTypedAction(); TypedAction typedAction = actionManager.getTypedAction();
typedAction.setupHandler(new MyTypedHandler()); typedAction.setupHandler(new MyTypedHandler());
} }
} }
``` ```

View File

@ -54,12 +54,13 @@ Similarly, to obtain a project reference, we use the `getProject()` method.
```java ```java
public class EditorIllustrationAction extends AnAction { public class EditorIllustrationAction extends AnAction {
@Override @Override
public void update(@NotNull final AnActionEvent e) { public void update(@NotNull final AnActionEvent e) {
// Get required data keys // Get required data keys
final Project project = e.getProject(); Project project = e.getProject();
final Editor editor = e.getData(CommonDataKeys.EDITOR); Editor editor = e.getData(CommonDataKeys.EDITOR);
} // ...
}
} }
``` ```
@ -78,10 +79,10 @@ Here's how the `EditorIllustrationAction.update(AnActionEvent e)` method should
```java ```java
public class EditorIllustrationAction extends AnAction { public class EditorIllustrationAction extends AnAction {
@Override @Override
public void update(@NotNull final AnActionEvent e) { public void update(@NotNull AnActionEvent e) {
// Get required data keys // Get required data keys
final Project project = e.getProject(); Project project = e.getProject();
final Editor editor = e.getData(CommonDataKeys.EDITOR); Editor editor = e.getData(CommonDataKeys.EDITOR);
// Set visibility only in the case of // Set visibility only in the case of
// existing project editor, and selection // existing project editor, and selection
@ -131,8 +132,8 @@ public class EditorIllustrationAction extends AnAction {
@Override @Override
public void actionPerformed(@NotNull final AnActionEvent e) { public void actionPerformed(@NotNull final AnActionEvent e) {
// Get all the required data from data keys // Get all the required data from data keys
final Editor editor = e.getRequiredData(CommonDataKeys.EDITOR); Editor editor = e.getRequiredData(CommonDataKeys.EDITOR);
final Project project = e.getRequiredData(CommonDataKeys.PROJECT); Project project = e.getRequiredData(CommonDataKeys.PROJECT);
final Document document = editor.getDocument(); final Document document = editor.getDocument();
// Work off of the primary caret to get the selection info // Work off of the primary caret to get the selection info