Skip to content

Commit c9140d5

Browse files
committed
Move to struct declaration: handle struct declaration via _var_ case
Replace single struct declaration with short var declaration, or add to existing struct literal in statement
1 parent 00f9d70 commit c9140d5

14 files changed

+285
-76
lines changed

src/com/goide/intentions/GoMoveToStructInitializationIntention.java

Lines changed: 148 additions & 76 deletions
Large diffs are not rendered by default.

src/com/goide/psi/impl/GoElementFactory.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,10 @@ public static GoTypeDeclaration createTypeDeclaration(@NotNull Project project,
266266
GoFile file = createFileFromText(project, "package a; type " + name + " " + type.getText());
267267
return PsiTreeUtil.findChildOfType(file, GoTypeDeclaration.class);
268268
}
269+
270+
@NotNull
271+
public static GoCompositeLit createCompositeLit(@NotNull Project project, @NotNull GoType type) {
272+
GoFile file = createFileFromText(project, "package a; var _ = " + type.getText() + "{}");
273+
return PsiTreeUtil.findChildOfType(file, GoCompositeLit.class);
274+
}
269275
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
var s string
9+
s.foo <caret>= "bar"
10+
print(s.foo)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
s := S{foo: "bar"}
9+
10+
print(s.foo)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
var (s S)
9+
s.foo <caret>= "bar"
10+
print(s.foo)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
s := S{foo: "bar"}
9+
10+
print(s.foo)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
var s S
9+
s.foo <caret>= "bar"
10+
print(s.foo)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
var s, b S
9+
s.foo <caret>= "bar"
10+
print(s.foo)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
var s S = S{foo: "bar"}
9+
10+
print(s.foo)
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
var s S = S{}
9+
s.foo <caret>= "bar"
10+
print(s.foo)
11+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
bar string
6+
}
7+
8+
func main() {
9+
var s S = S{bar: "a", foo: "bar"}
10+
11+
print(s.foo)
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
bar string
6+
}
7+
8+
func main() {
9+
var s S = S{bar: "a"}
10+
s.foo <caret>= "bar"
11+
print(s.foo)
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
type S struct {
4+
foo string
5+
}
6+
7+
func main() {
8+
var s S
9+
var b S
10+
s.foo <caret>= "bar"
11+
print(b.foo)
12+
}

tests/com/goide/quickfix/GoMoveToStructInitializationIntentionTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,23 @@ protected String getBasePath() {
5050
public void testMultipleFieldsPartlyAssigned() { doTest(); }
5151
public void testWithParens() { doTest(); }
5252
public void testFieldExtractedFromParens() { doTest(); }
53+
public void testStructDeclaration() { doTest(); }
54+
public void testParensStructDeclaration() { doTest(); }
55+
public void testStructDeclarationWithEmptyInit() { doTest(); }
56+
public void testStructDeclarationWithNonEmptyInit() { doTest(); }
5357

5458
public void testDuplicateFields() { doTest(); }
5559
public void testMultiReturnFunction() { doTestNoFix(); }
5660
public void testWrongStruct() { doTestNoFix(); }
61+
public void testNotStructDeclaration() { doTestNoFix(); }
5762
public void testExistingDeclaration() { doTestNoFix(); }
5863
public void testNotExistingField() { doTestNoFix(); }
5964
public void testJustAssignedVarWrongCaret() { doTestNoFix(); }
6065
public void testJustAssignedVarWrongCaretWithParens() { doTestNoFix(); }
6166
public void testJustInitializedVarWrongCaret() { doTestNoFix(); }
6267
public void testJustAssignedVarBothParens() { doTestNoFix(); }
6368
public void testJustAssignedFieldParens() { doTestNoFix(); }
69+
public void testStructDeclarationMultipleExpressions() { doTestNoFix(); }
70+
public void testStructDeclarationWrongQualifier() { doTestNoFix(); }
6471
}
6572

0 commit comments

Comments
 (0)