summaryrefslogtreecommitdiffstats
path: root/lua/snippets/java.lua
blob: 842462897a3d1bdbd35b5712a3104e62d69dbb9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require('core.snippets.shorthands')

local function file_name(args, parent, user_args)
  return vim.fn.expand("%:t:r")
end

return {
  -- method snippet
  s("fn", {
    c(1, {
      t("public "),
      t("private "),
    }),
    c(2, {
      t("void"),
      t("String"),
      t("char"),
      t("int"),
      t("double"),
      t("boolean"),
      i(nil, "myType"),
    }),
    t(" "),
    i(3, "myFunc"),
    t("("),
    i(4),
    t(")"),
    c(5, {
      t(""),
      sn(nil, {t({ "", " throws " }), i(1)}),
    }),
    t({ " {", "\t" }),
    i(0),
    t({ "", "}" }),
  }),

  -- constructor snippet
  s("constr", {
    c(1, {
      t("public "),
      t("private "),
      t("protected ")
    }),
    f(file_name, {}),
    t("("),
    i(2),
    t(")"),
    c(3, {
      t(""),
      sn(nil, {t({ "", " throws " }), i(1)}),
    }),
    t({ " {", "\t" }),
    i(0),
    t({ "", "}" }),
  }),

  -- class snippet
  s("class", {
    c(1, {
      t("public "),
      t("private "),
      t("protected ")
    }),
    c(2, {
      t("class "),
      t("interface ")
    }),
    f(file_name, {}),
    c(3, {
      t(" "),
      sn(nil, { t({" implements "}), i(1), t(" ") }),
      sn(nil, { t({" extends "}), i(1), t(" ") }),
    }),
    t({ "{", "\t" }),
    i(0),
    t({ "", "}" }),
  }),

  -- pacakge snippet
  s("package", {
    t("package "),
    f(function(args, parent, user_args)
      -- get path
      local dir = vim.fn.expand("%:h")
      -- remove prefix
      dir = dir:gsub("src/main/java/", "")
      -- convert to package path
      dir = dir:gsub("/", ".")

      return dir
    end, {}),
    t(";"),
    t({ "", "", "" })
  }),

  -- try, catch, finally snippet
  s("try", {
    t({"try {", "\t"}),
    i(1),
    t({"", "} catch ("}),
    i(2, "Error e"),
    t({") {", "\t"}),
    i(3),
    t({"", "}"}),
    c(4, {
      t(""),
      sn(nil, { t({" finally {", "\t"}), i(1), t({"", "}"}) })
    })
  })
}