MCPcopy Create free account
hub / github.com/cogentcore/core / Code

Method Code

shell/token.go:135–198  ·  view source on GitHub ↗

Code returns concatenated Str values of the tokens, to generate a surface-valid code string.

()

Source from the content-addressed store, hash-verified

133// Code returns concatenated Str values of the tokens,
134// to generate a surface-valid code string.
135func (tk Tokens) Code() string {
136 n := len(tk)
137 if n == 0 {
138 return ""
139 }
140 str := ""
141 prvIdent := false
142 for _, tok := range tk {
143 switch {
144 case tok.IsOp():
145 if tok.Tok == token.INC || tok.Tok == token.DEC {
146 str += tok.String() + " "
147 } else if tok.Tok == token.MUL {
148 str += " " + tok.String()
149 } else {
150 str += " " + tok.String() + " "
151 }
152 prvIdent = false
153 case tok.Tok == token.ELLIPSIS:
154 str += " " + tok.String()
155 prvIdent = false
156 case tok.IsBracket() || tok.Tok == token.PERIOD:
157 if tok.Tok == token.RBRACE || tok.Tok == token.LBRACE {
158 if len(str) > 0 && str[len(str)-1] != ' ' {
159 str += " "
160 }
161 str += tok.String() + " "
162 } else {
163 str += tok.String()
164 }
165 prvIdent = false
166 case tok.Tok == token.COMMA || tok.Tok == token.COLON || tok.Tok == token.SEMICOLON:
167 str += tok.String() + " "
168 prvIdent = false
169 case tok.Tok == token.STRUCT:
170 str += " " + tok.String() + " "
171 case tok.Tok == token.FUNC:
172 if prvIdent {
173 str += " "
174 }
175 str += tok.String()
176 prvIdent = false
177 case tok.IsGo():
178 str += tok.String() + " "
179 prvIdent = false
180 case tok.Tok == token.IDENT || tok.Tok == token.STRING:
181 if prvIdent {
182 str += " "
183 }
184 str += tok.String()
185 prvIdent = true
186 default:
187 str += tok.String()
188 prvIdent = false
189 }
190 }
191 if len(str) == 0 {
192 return str

Callers

nothing calls this directly

Calls 4

IsOpMethod · 0.80
IsBracketMethod · 0.80
IsGoMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected