MCPcopy Index your code
hub / github.com/idank/explainshell / visitcommand

Method visitcommand

explainshell/matcher.py:260–314  ·  view source on GitHub ↗
(self, node, parts)

Source from the content-addressed store, hash-verified

258 return False
259
260 def visitcommand(self, node, parts):
261 assert parts
262
263 # look for the first WordNode, which might not be at parts[0]
264 idx_word_node = bashlex.ast.findfirstkind(parts, "word")
265 if idx_word_node == -1:
266 logger.info("no words found in command (probably contains only redirects)")
267 return
268
269 word_node = parts[idx_word_node]
270
271 # check if this refers to a previously defined function
272 if word_node.word in self.functions:
273 logger.info(
274 f"word {word_node} is a function, not trying to match it or it's arguments"
275 )
276
277 # first, add a MatchResult for the function call
278 mr = MatchResult(
279 word_node.pos[0],
280 word_node.pos[1],
281 help_constants._function_call % word_node.word,
282 None,
283 {"kind": "function_call"},
284 )
285 self.matches.append(mr)
286
287 # this is a bit nasty: if we were to visit the command like we
288 # normally do it would try to match it against a manpage. but
289 # we don't have one here, we just want to take all the words and
290 # consider them part of the function call
291 for part in parts:
292 # maybe it's a redirect...
293 if part.kind != "word":
294 self.visit(part)
295 else:
296 # this is an argument to the function
297 if part is not word_node:
298 mr = MatchResult(
299 part.pos[0],
300 part.pos[1],
301 help_constants._functionarg % word_node.word,
302 None,
303 {"kind": "function_arg"},
304 )
305 self.matches.append(mr)
306
307 # visit any expansions in there
308 for p_part in part.parts:
309 self.visit(p_part)
310
311 # we're done with this commandnode, don't visit its children
312 return False
313
314 self.startcommand(node, parts, None)
315
316 def visitif(self, *args):
317 self.compound_stack.append("if")

Callers

nothing calls this directly

Calls 2

startcommandMethod · 0.95
MatchResultClass · 0.85

Tested by

no test coverage detected