Skip to content

Commit 8f48f59

Browse files
committed
✨ Support Inline Conditions.
1 parent cb8a0c5 commit 8f48f59

File tree

6 files changed

+55
-12
lines changed

6 files changed

+55
-12
lines changed

.DS_Store

-6 KB
Binary file not shown.

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
## [1.2.1] - 2023-07-19
88

9+
### Added
10+
11+
- Support Inline Conditions.
12+
913
### Fixed
1014

1115
- Fix GoFrame ORM unsupported syntax.

example/goframe/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ type User struct {
1212
Age int32 // 年龄
1313
}
1414

15-
func main() {
15+
func test1() (user *User) {
1616
g.Model(&User{}).Where("id = ?")
17-
}
1817

19-
func test1() (user *User) {
18+
g.Model(&User{}).Where("id = ? and user_name = ? OR email != ?")
19+
20+
g.DB().Model(&User{}).Where("id = ? and")
21+
2022
g.Model(&user).Where("id = ?", 1)
2123

2224
g.Model(&User{}).Where(g.Map{"user_name": 1, "name": "john"})

example/gorm/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,9 @@ func test6(db *gorm.DB) (user *User) {
7979
query.Find(&user)
8080
return
8181
}
82+
83+
func test7(db *gorm.DB) (user *User) {
84+
query := db.Where("id = ? and email != ? OR user_name = ?", "")
85+
query.Find(&user)
86+
return
87+
}

src/main/java/com/github/maiqingqiang/goormhelper/Types.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public interface Types {
2929
"%s BETWEEN ? AND ?",
3030
"%s NOT BETWEEN ? AND ?"
3131
);
32+
33+
List<String> LOGICAL_OPERATOR_EXPR = List.of("AND", "OR", "XOR", "NOT");
34+
List<String> USE_LOGICAL_OPERATOR_SCENE = List.of(" ?", " IS NULL", " IS NOT NULL");
35+
3236
String MODEL_ANNOTATION = "@Model";
3337
Pattern MODEL_ANNOTATION_PATTERN = Pattern.compile(MODEL_ANNOTATION + "\\((.*?)\\)");
3438
String TABLE_ANNOTATION = "@Table";

src/main/java/com/github/maiqingqiang/goormhelper/orm/ORMCompletionProvider.java

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.intellij.codeInsight.completion.CompletionParameters;
1515
import com.intellij.codeInsight.completion.CompletionProvider;
1616
import com.intellij.codeInsight.completion.CompletionResultSet;
17-
import com.intellij.codeInsight.completion.CompletionUtil;
1817
import com.intellij.codeInsight.lookup.LookupElementBuilder;
1918
import com.intellij.openapi.diagnostic.Logger;
2019
import com.intellij.openapi.project.Project;
@@ -26,6 +25,7 @@
2625
import com.intellij.psi.ResolveState;
2726
import com.intellij.psi.util.PsiTreeUtil;
2827
import com.intellij.util.ProcessingContext;
28+
import org.apache.commons.lang3.StringUtils;
2929
import org.jetbrains.annotations.NotNull;
3030
import org.jetbrains.annotations.Nullable;
3131

@@ -40,7 +40,6 @@ public abstract class ORMCompletionProvider extends CompletionProvider<Completio
4040
private static final Logger LOG = Logger.getInstance(ORMCompletionProvider.class);
4141

4242
protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet result) {
43-
4443
PsiElement currentElement = parameters.getPosition();
4544

4645
LOG.info("currentElement: " + currentElement + " text: " + currentElement.getText());
@@ -59,6 +58,37 @@ protected void addCompletions(@NotNull CompletionParameters parameters, @NotNull
5958
if (!ORMPsiTreeUtil.callHasArgumentAtIndex(goCallExpr, argumentIndex, currentElement.getParent()) && !(currentElement.getParent().getParent() instanceof GoKey))
6059
return;
6160

61+
String prefix = result.getPrefixMatcher().getPrefix();
62+
int lastSpace = prefix.lastIndexOf(' ');
63+
if (lastSpace >= 0 && lastSpace < prefix.length() - 1) {
64+
String previous = prefix.substring(0, lastSpace);
65+
LOG.info("previous: " + previous + "origin prefix: " + prefix);
66+
67+
prefix = prefix.substring(lastSpace + 1);
68+
LOG.info("new prefix: " + prefix);
69+
70+
result = result.withPrefixMatcher(prefix);
71+
72+
if (StringUtils.containsAnyIgnoreCase(previous, Types.USE_LOGICAL_OPERATOR_SCENE.toArray(new CharSequence[]{}))) {
73+
for (String s : Types.LOGICAL_OPERATOR_EXPR) {
74+
if (StringUtils.containsIgnoreCase(s, prefix)) {
75+
result.addElement(LookupElementBuilder
76+
.create(s)
77+
.withPresentableText(s)
78+
.withIcon(getIcon()));
79+
80+
String lowerCase = s.toLowerCase();
81+
82+
result.addElement(LookupElementBuilder
83+
.create(lowerCase)
84+
.withPresentableText(lowerCase)
85+
.withIcon(getIcon()));
86+
}
87+
}
88+
return;
89+
}
90+
}
91+
6292
GoCompositeElement argument = findTargetGoCompositeElement(currentElement);
6393

6494
LOG.info("argument: " + argument);
@@ -352,20 +382,17 @@ private void scanFields(@NotNull CompletionParameters parameters, GoCallableDesc
352382
comment = GoDocumentationProvider.getCommentText(GoDocumentationProvider.getCommentsForElement(field), false);
353383
}
354384

355-
GoStringLiteral goStringLiteral = (GoStringLiteral) parameters.getPosition().getParent();
356-
String currentString = goStringLiteral.getDecodedText().replace(CompletionUtil.DUMMY_IDENTIFIER, "");
357-
358-
if (column != null && !column.contains(currentString.trim())) continue;
385+
if (column != null && !column.contains(result.getPrefixMatcher().getPrefix())) continue;
359386

360-
addElement(parameters, result, column, comment, type, goTypeSpec);
387+
addElement(result, column, comment, type, goTypeSpec);
361388

362389
if (!(parameters.getPosition().getParent().getParent() instanceof GoKey)) {
363390
Map<GoCallableDescriptor, List<String>> queryExpr = queryExpr();
364391
if (queryExpr != null) {
365392
List<String> whereExpr = queryExpr.get(descriptor);
366393
if (whereExpr != null) {
367394
for (String s : whereExpr) {
368-
addElement(parameters, result, String.format(s, column), comment, type, goTypeSpec);
395+
addElement(result, String.format(s, column), comment, type, goTypeSpec);
369396
}
370397
}
371398
}
@@ -374,7 +401,7 @@ private void scanFields(@NotNull CompletionParameters parameters, GoCallableDesc
374401
}
375402
}
376403

377-
private void addElement(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result, String column, String comment, String type, @NotNull GoTypeSpec goTypeSpec) {
404+
private void addElement(@NotNull CompletionResultSet result, String column, String comment, String type, @NotNull GoTypeSpec goTypeSpec) {
378405
LookupElementBuilder builder = LookupElementBuilder
379406
.createWithSmartPointer(column, goTypeSpec)
380407
.withPresentableText(column)

0 commit comments

Comments
 (0)