@@ -125,16 +125,19 @@ def on_query_completions(self, view, prefix, locations):
125125 'fn' : view .file_name () or '' ,
126126 }
127127 show_snippets = gs .setting ('autocomplete_snippets' , True ) is True
128+ fn = view .file_name () or '<stdin>'
128129
129- if not pkgname :
130+ if not fn . endswith ( ".gohtml" ) and not pkgname :
130131 return (resolve_snippets (ctx ), AC_OPTS ) if show_snippets else ([], AC_OPTS )
131132
132133 # gocode is case-sesitive so push the location back to the 'dot' so it gives
133134 # gives us everything then st2 can pick the matches for us
134135 offset = pos - len (prefix )
135136 src = view .substr (sublime .Region (0 , view .size ()))
136137
137- fn = view .file_name () or '<stdin>'
138+ if fn .endswith (".gohtml" ):
139+ src , offset = self .get_gohtml_src (default_pkgname , src , pos )
140+
138141 if not src :
139142 return ([], AC_OPTS )
140143
@@ -151,6 +154,61 @@ def on_query_completions(self, view, prefix, locations):
151154 cl .extend (resolve_snippets (ctx ))
152155 return (cl , AC_OPTS )
153156
157+ def get_extra_gohtml_src (self , src , i , pos ):
158+ extra_src = u""
159+ try :
160+ i = src .index ("@{" , i )
161+ except ValueError :
162+ return extra_src
163+
164+ counter = 0
165+ while i < pos :
166+ j = src .index ("\n " , i )
167+ if j >= pos :
168+ break
169+
170+ if not src [i ] == "<" :
171+ i += 1
172+ line = src [i :j + 1 ].strip ()
173+ if not line .startswith ("<" ) and line != "{" :
174+ extra_src += line + "\n "
175+ if line .endswith ("{" ):
176+ counter += 1
177+ if line .startswith ("}" ):
178+ counter -= 1
179+
180+ if line == "}" and counter == 0 :
181+ try :
182+ i = src .index ("@{" , j )
183+ except ValueError :
184+ return extra_src
185+ else :
186+ i = j + 1
187+
188+ return extra_src
189+
190+
191+ def get_gohtml_src (self , default_pkgname , src , pos ):
192+ if src == None or not src .startswith ("@{" ):
193+ return None , 0
194+
195+ i = src .rindex ("\n " , 0 , pos )
196+ line = src [i :pos ].strip ()
197+ if line .startswith ("@" ):
198+ line = u"\n " + line [1 :]
199+ else :
200+ line = u"\n " + line
201+
202+ i = src .index ("}" )
203+ extra_src = self .get_extra_gohtml_src (src , i , pos )
204+ go_source = "package " + default_pkgname + "\n " + src [2 :i ]
205+ i = go_source .index (")" ) + 1
206+
207+ go_source = go_source [0 :i ] + "\n func main(){\n raw := func(string)string{}\n " + go_source [i :] + extra_src + line + "\n }"
208+ offset = len (go_source ) - 2
209+
210+ return go_source , offset
211+
154212 def find_end_pt (self , view , pat , start , end , flags = sublime .LITERAL ):
155213 r = view .find (pat , start , flags )
156214 return r .end () if r and r .end () < end else - 1
0 commit comments