-
-
Notifications
You must be signed in to change notification settings - Fork 397
Add /skript command tab completer #3916
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TPGamesNL
merged 19 commits into
SkriptLang:master
from
AyhamAl-Ali:feature/skript-cmd-tab-completer
Nov 6, 2021
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4841c5c
Add /skript Command Tab Completer
AyhamAl-Ali 9e86a7a
Remove unused imports
AyhamAl-Ali e805c0e
Improved completions
AyhamAl-Ali 4493049
Remove duplicated license
AyhamAl-Ali 8c02be5
Restore directories slash
AyhamAl-Ali 1a20fd3
Fix disabled/hidden sub-directories not filtered
AyhamAl-Ali b9be2a5
Changes as requested + some more
AyhamAl-Ali 7e6023e
Merge branch 'master' into feature/skript-cmd-tab-completer
AyhamAl-Ali 59a5c21
Merge branch 'master' into feature/skript-cmd-tab-completer
TPGamesNL 257243e
Update files
AyhamAl-Ali db483e4
Merge branch 'master' into feature/skript-cmd-tab-completer
AyhamAl-Ali b8a0e7c
Update files
AyhamAl-Ali 141cd49
Merge remote-tracking branch 'AyhamAl-Ali/feature/skript-cmd-tab-comp…
AyhamAl-Ali 80a74cf
Update files once more :D
AyhamAl-Ali ae9cb8e
Another one
AyhamAl-Ali 77503cc
Actually fix too early tab completions
AyhamAl-Ali 11c79a2
The actual fix 😬
AyhamAl-Ali 77c6250
Merge branch 'master' into feature/skript-cmd-tab-completer
TPGamesNL bda76bc
Remove redundant check
AyhamAl-Ali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
109 changes: 109 additions & 0 deletions
109
src/main/java/ch/njol/skript/SkriptCommandTabCompleter.java
This file contains hidden or 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,109 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||
*/ | ||
package ch.njol.skript; | ||
|
||
import ch.njol.skript.tests.runner.TestMode; | ||
import ch.njol.util.StringUtils; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.command.TabCompleter; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class SkriptCommandTabCompleter implements TabCompleter { | ||
|
||
@Override | ||
@Nullable | ||
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) { | ||
ArrayList<String> options = new ArrayList<>(); | ||
|
||
if (!command.getName().equalsIgnoreCase("skript")) { | ||
return null; | ||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
if (args[0].equalsIgnoreCase("update") && args.length == 2) { | ||
options.add("check"); | ||
options.add("changes"); | ||
options.add("download"); | ||
} else if (args[0].matches("(?i)(reload|disable|enable)") && args.length == 2) { | ||
File scripts = new File(Skript.getInstance().getDataFolder(), Skript.SCRIPTSFOLDER); | ||
String scriptArg = StringUtils.join(args, " ", 1, args.length); | ||
String fs = File.separator; | ||
|
||
try { | ||
// Live update, this will get all old and new (even not loaded) scripts | ||
Files.walk(scripts.toPath()) | ||
.map(Path::toFile) | ||
.filter(f -> (!f.isDirectory() && f.getName().toLowerCase().endsWith(".sk")) || | ||
f.isDirectory()) // filter folders and skript files only | ||
.filter(f -> { // Filtration for enable, disable and reload | ||
if (args[0].equalsIgnoreCase("enable")) | ||
return f.getName().startsWith("-"); | ||
else // reload & disable both accepts only non-hyphened files and not hidden folders | ||
return !f.getAbsolutePath().matches(".*?(\\\\-|/-|^-).*") && (f.isDirectory() && | ||
!f.getAbsolutePath().matches(".*?(\\\\\\.|/\\.|^\\.).*") || !f.isDirectory()); | ||
}) | ||
.filter(f -> { // Autocomplete incomplete script name arg | ||
return scriptArg.length() > 0 ? f.getName().startsWith(scriptArg) : true; | ||
}) | ||
.forEach(f -> { | ||
if (!f.toString().equals(scripts.toString())) | ||
options.add(f.toString() | ||
.replace(scripts.toPath() + (!f.isDirectory() && f.getParentFile().toPath().toString() | ||
.equals(scripts.toPath().toString()) ? fs : ""), "") // Extract file short path, and remove '/' from the beginning of files in root only | ||
+ (f.isDirectory() && f.toString().length() > 0 ? fs : "")); // add File.separator at the end of directories | ||
}); | ||
|
||
// TODO handle file permissions | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// These will be added even if there are incomplete script arg | ||
options.add("all"); | ||
if (args[0].equalsIgnoreCase("reload")) { | ||
options.add("config"); | ||
options.add("aliases"); | ||
options.add("scripts"); | ||
} | ||
} else if (args.length == 1) { | ||
options.add("help"); | ||
AyhamAl-Ali marked this conversation as resolved.
Show resolved
Hide resolved
|
||
options.add("reload"); | ||
options.add("enable"); | ||
options.add("disable"); | ||
options.add("update"); | ||
options.add("info"); | ||
if (new File(Skript.getInstance().getDataFolder() + "/doc-templates").exists()) { | ||
options.add("gen-docs"); | ||
} | ||
if (TestMode.DEV_MODE) { | ||
options.add("test"); | ||
} | ||
} | ||
|
||
return options; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.