python-editor-v3 icon indicating copy to clipboard operation
python-editor-v3 copied to clipboard

[Code structure highlighting] Function bodies not highlighted when first statement is an assignment

Open microbit-matt-hillsdon opened this issue 2 years ago • 4 comments

Split out from https://github.com/microbit-foundation/python-editor-v3/issues/809.

image

d, f, and g are incorrectly highlighted. It would be interesting to see what CodeMirror's parse tree is in this case.

This seems likely to arise in practice as initialising locals is common at the start of a function.

microbit-matt-hillsdon avatar Oct 07 '22 13:10 microbit-matt-hillsdon

def a():
    pass

def b():
    print("Hello")

def c():
    print("Hello")

def d(x):
    x = 1

def e(x):
    x += 1

def f():
    x, y = 1, 2

def g():
    a = 1
    print()

microbit-matt-hillsdon avatar Oct 07 '22 13:10 microbit-matt-hillsdon

This occurs whenever a variable is unused in a function. Assignment can happen first, as long as the variable assigned is referenced later. Similarly, if the function takes an argument, and that argument is unused in the function, highlighting also breaks. Essentially, the var_name is unused hint/info diagnostic is interfering with the highlighting.

microbit-robert avatar Oct 10 '22 08:10 microbit-robert

We intentionally hide code structure highlighting that overlaps with unused code diagnostics. Perhaps "overlaps" was the wrong call. We need to revisit and set up some scenarios to see how they look. I think we had things like content after while True in mind rather than unused locals.

microbit-matt-hillsdon avatar Nov 10 '22 08:11 microbit-matt-hillsdon

It looks like we want to hide code structure highlighting for unreachable code, rather than unused code. Pyright bundles both unused and unreachable code into the same tag, DiagnosticTag.Unnecessary. If we could extend DiagnosticTag to include something like "Unreachable", then there is very little to do on the Editor side.

microbit-robert avatar Nov 18 '22 17:11 microbit-robert