Skip to content

Commit

Permalink
Add custom AsciiDoc search scopes (#1216)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahus1 committed Nov 16, 2022
1 parent 921dda4 commit dc501d3
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This document provides a high-level view of the changes introduced by release.

- Require IntelliJ 2021.3.2 as minimum version update deprecated APIs
- Add an action to open the dev tools for the JCEF preview
- Add custom AsciiDoc search scopes (#1216)

=== 0.37.54

Expand Down
1 change: 1 addition & 0 deletions doc/users-guide/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* xref:features.adoc[]
** xref:features/editor.adoc[]
** xref:features/keymap.adoc[]
** xref:features/search.adoc[]
** xref:features/preview.adoc[]
*** xref:features/preview/diagrams.adoc[]
*** xref:features/preview/javafx-preview.adoc[]
Expand Down
7 changes: 7 additions & 0 deletions doc/users-guide/modules/ROOT/pages/features.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ The IntelliJ AsciiDoc Plugin adds to your IDE:

Learn more about it in the xref:features/editor.adoc[] section.

== Advanced search functionality

* All named elements can be searched, like section titles, IDs, and attribute names.
* Two additional search scopes to filter searching in AsciiDoc content.

Learn more about it in the xref:features/search.adoc[] section.

== Configurable preview

* Provides a two-pane AsciiDoc preview editor based on https://github.com/asciidoctor/asciidoctorj[AsciidoctorJ] with a live preview.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Use the icon icon:question[] to read the docs of the IDE to find out about addit

. In the pop-up window, the directory is preselected.
Keep this selection, or choose a different scope or the whole project depending on where files should be inspected.
+
The plugin also adds two custom scopes: _AsciiDoc files_ and _AsciiDoc files without Symlinks_, which are applied to the whole project.
The scope without symlinks avoids validating a file multiple times with duplicate error messages.
In this scope, it will only be validated once in its original location.
While this avoids duplicate error messages, it might also hide error messages that are context dependent on the symlink location.

. When running this for the first time, select menu:Configure...[] to select which kind of checks to run.
+
Expand Down
2 changes: 2 additions & 0 deletions doc/users-guide/modules/ROOT/pages/features/preview.adoc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
= Previewing content
:description: Per default, the preview is on the right side of a vertical split screen and shows the rendered contents of the editor.

{description}

== Overview

By default, the preview is on the right side of a vertical split screen.
Expand Down
22 changes: 22 additions & 0 deletions doc/users-guide/modules/ROOT/pages/features/search.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
= Searching content
:description: All AsciiDoc content is indexed, which allows fast searching.

{description}

== Overview

There is standard functionality to search in IntelliJ:

Users can search in a single file using kbd:[Ctrl+F].
Replacing is done via kbd:[Ctrl+R].

Users can search in all files using kbd:[Ctrl+Shift+F].
By default, the search is case-insensitive and searches for text snippets.
It can be changed to search case-sensitive and with regexes.
Replacing is available via kbd:[Ctrl+Shift+R].

When searching in the project, and also renaming entities, the plugin adds two new search scopes: _AsciiDoc files_ and _AsciiDoc files without Symlinks_.

Using kbd:[Shift+Shift] (search everything), the user can search for all named elements. By default, the search returns file names.
The plugin adds the capability to include section headings, attribute names, tags and IDs.

27 changes: 26 additions & 1 deletion src/main/java/org/asciidoc/intellij/psi/AsciiDocSearchScope.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.asciidoc.intellij.file.AsciiDocFileType;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Collection;

/**
Expand All @@ -20,6 +22,7 @@
public class AsciiDocSearchScope extends GlobalSearchScope {

private final FileIndexFacade myFileIndexFacade;
private boolean excludeSymLinks;

public AsciiDocSearchScope(Project project) {
super(project);
Expand All @@ -30,12 +33,34 @@ public GlobalSearchScope restrictedByAsciiDocFileType() {
return GlobalSearchScope.getScopeRestrictedByFileTypes(this, AsciiDocFileType.INSTANCE);
}

public AsciiDocSearchScope excludeSymlinks() {
this.excludeSymLinks = true;
return this;
}

@Override
public boolean contains(@NotNull VirtualFile file) {
// even if isSearchInLibraries returns false, the check for isInLibraryXXX is still needed
return !myFileIndexFacade.isExcludedFile(file) &&
boolean result = !myFileIndexFacade.isExcludedFile(file) &&
!myFileIndexFacade.isInLibraryClasses(file) &&
!myFileIndexFacade.isInLibrarySource(file);

if (result && excludeSymLinks) {
if (file.isInLocalFileSystem()) {
Path path = Path.of(file.getPath());
Path realPath;
try {
realPath = path.toRealPath();
if (!realPath.toString().equals(path.toString())) {
result = false;
}
} catch (IOException e) {
// ignored
}
}
}

return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.asciidoc.intellij.searchScopes;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.project.Project;
import com.intellij.psi.search.SearchScope;
import com.intellij.psi.search.SearchScopeProvider;
import org.asciidoc.intellij.psi.AsciiDocSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class AsciiDocSearchScopeProvider implements SearchScopeProvider {
@Override
public @Nullable String getDisplayName() {
return "AsciiDoc";
}

@Override
public @NotNull List<SearchScope> getSearchScopes(@NotNull Project project, @NotNull DataContext dataContext) {
ArrayList<SearchScope> result = new ArrayList<>();
result.add(new AsciiDocSearchScopeWithName(new AsciiDocSearchScope(project).excludeSymlinks(), "All files without Symlinks"));
result.add(new AsciiDocSearchScopeWithName(new AsciiDocSearchScope(project).restrictedByAsciiDocFileType(), "AsciiDoc files"));
result.add(new AsciiDocSearchScopeWithName(new AsciiDocSearchScope(project).excludeSymlinks().restrictedByAsciiDocFileType(), "AsciiDoc files without Symlinks"));
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.asciidoc.intellij.searchScopes;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.psi.search.SearchScope;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.Objects;

public class AsciiDocSearchScopeWithName extends GlobalSearchScope {

private final GlobalSearchScope searchScope;
private final String name;

public AsciiDocSearchScopeWithName(GlobalSearchScope searchScope, String name) {
this.searchScope = searchScope;
this.name = name;
}

@Override
protected int calcHashCode() {
return Objects.hash(name);
}

@SuppressWarnings("checkstyle:EqualsHashCode")
@Override
public boolean equals(Object obj) {
if (!(obj instanceof AsciiDocSearchScopeWithName)) {
return false;
}
return Objects.equals(name, ((AsciiDocSearchScopeWithName) obj).name);
}

@Override
@Nls(capitalization = Nls.Capitalization.Sentence)
@NotNull
public String getDisplayName() {
return name;
}

@Override
public @Nullable Icon getIcon() {
return searchScope.getIcon();
}

@Override
public boolean isSearchInModuleContent(@NotNull Module aModule) {
return searchScope.isSearchInModuleContent(aModule);
}

@Override
public boolean isSearchInLibraries() {
return searchScope.isSearchInLibraries();
}

@Override
@Contract(pure = true)
@NotNull
public SearchScope intersectWith(@NotNull SearchScope scope2) {
return searchScope.intersectWith(scope2);
}

@Override
@Contract(pure = true)
@NotNull
public GlobalSearchScope union(@NotNull SearchScope scope) {
return searchScope.union(scope);
}

@Override
@Contract(pure = true)
public boolean contains(@NotNull VirtualFile file) {
return searchScope.contains(file);
}

}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
key="settings.asciidoc.preview.name"
instance="org.asciidoc.intellij.settings.AsciiDocPreviewConfigurable">
</applicationConfigurable>
<searchScopesProvider implementation="org.asciidoc.intellij.searchScopes.AsciiDocSearchScopeProvider" />
<applicationService serviceImplementation="org.asciidoc.intellij.settings.AsciiDocApplicationSettings"/>
<editorNotificationProvider
implementation="org.asciidoc.intellij.editor.javafx.notification.JavaFxCouldBeEnabledNotificationProvider"/>
Expand Down

0 comments on commit dc501d3

Please sign in to comment.