From 63303a39da9cd063a994168b72efea6e7ae6656d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=B8=87=E7=A8=8B?= <1498425439@qq.com> Date: Sat, 9 Apr 2022 22:46:31 +0800 Subject: [PATCH] =?UTF-8?q?COBOL=20NOT=20AND=20OR=20|=20=E7=89=B9=E6=AE=8A?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/show/ext/LineExtUtils.java | 23 +++++++++++++++++++ .../show/demo/ext/cobol/COBOL.cbl.key.tsv | 8 +++---- .../plugin/show/demo/ext/cobol/demo/BASE.cbl | 3 ++- .../show/demo/ext/cobol/doc/cbl.doc.tsv | 2 +- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/linwancen/plugin/show/ext/LineExtUtils.java b/src/main/java/io/github/linwancen/plugin/show/ext/LineExtUtils.java index 8c78c73..3d2441a 100644 --- a/src/main/java/io/github/linwancen/plugin/show/ext/LineExtUtils.java +++ b/src/main/java/io/github/linwancen/plugin/show/ext/LineExtUtils.java @@ -34,11 +34,34 @@ public class LineExtUtils { } Map>> treeMap = ConfCache.treeMap(project, file); String text = document.getText(new TextRange(startOffset, endOffset)); + if ("cbl".equals(file.getExtension())) { + text = cblNotAndOr(text); + } String[] words = pattern.split(text); Matcher matcher = pattern.matcher(text); return extDoc(keyMap, matcher, docMap, words, treeMap, project); } + private static final Pattern DICT_PATTERN = Pattern.compile("([\\w-]++) ?(NOT)? ?= ?'"); + private static final Pattern AND_OR_PATTERN = Pattern.compile("(AND|OR) ?'"); + + @NotNull + private static String cblNotAndOr(String text) { + // maybe faster than regexp + if (!text.contains("=")) { + return text; + } + Matcher matcher = DICT_PATTERN.matcher(text); + if (!matcher.find()) { + return text; + } + String key = matcher.group(1); + // put NOT first + text = matcher.replaceAll("$2 ( $1 = '"); + // add key after AND/OR + return AND_OR_PATTERN.matcher(text).replaceAll("$1 "+ key + " = '"); + } + @Nullable private static String extDoc(@NotNull Map>> keyMap, @NotNull Matcher matcher, @NotNull Map>> docMap, @NotNull String[] words, diff --git a/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/COBOL.cbl.key.tsv b/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/COBOL.cbl.key.tsv index 35a2a62..35e3aa8 100644 --- a/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/COBOL.cbl.key.tsv +++ b/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/COBOL.cbl.key.tsv @@ -13,11 +13,11 @@ IF if 有些程序 THEN 没有换行所以要加这个关键字 THEN - NOT(?! '| ?=) ! - AND (?!') && - OR (?!') || + NOT(?= ) ! + AND(?= ) && + OR(?= ) || - 兼容单字符字典 + 兼容字符串字典 \=(?! ?') = > > < < diff --git a/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/demo/BASE.cbl b/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/demo/BASE.cbl index 75e2074..79c62a8 100644 --- a/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/demo/BASE.cbl +++ b/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/demo/BASE.cbl @@ -1,6 +1,7 @@ 123456 INITIALIZE HELLO-WORLD - IF KEY = '1' AND KEY NOT = '2' AND (B OR NOT C) THEN + IF (KEY NOT = '1' AND '2') + AND KEY NOT = '1' THEN MOVE WS-HELLO-WORLD TO HELLO-WORLD END IF TABLE(STRUCT) diff --git a/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/doc/cbl.doc.tsv b/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/doc/cbl.doc.tsv index 2fe6d80..c329d16 100644 --- a/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/doc/cbl.doc.tsv +++ b/src/test/java/io/github/linwancen/plugin/show/demo/ext/cobol/doc/cbl.doc.tsv @@ -1,5 +1,5 @@ KEY = '1' 字典1 -KEY NOT = '2' 非 字典2 +KEY = '2' 字典2 HELLO-WORLD 你好世界 B001-A 程序A A a