MagicPython icon indicating copy to clipboard operation
MagicPython copied to clipboard

Item access not only with variables

Open echasnovski opened this issue 5 years ago • 1 comments
trafficstars

Using item access with anything other than a variable isn't recognized as item access.

I have set "punctuation.definition.arguments" to a custom color in order to highlight brackets and parenthesis only if they are used for item access and function call respectively. However, it doesn't work in all cases when item access is actually done.

  • Editor name and version: VS Code 1.44.1
  • Platform: Ubuntu 18.04
  • Color scheme: Customized "Solarized Dark" (color of "punctuation.definition.arguments" is set to "#b58900")
  • MagicPython version: the one shipped with VS Code
  • A sreenshot: magicpython_item-access Expectation here is that all cases of [0] should have colored brackets.
  • 5-10 lines of surrounding code:
x = [1, 2, 3]
x[0]
sorted(x, reverse=True)[0]
(x)[0]
(sorted(x))[0]
("a", "b")[0]
["a", "b"][0]
{0: "a", 1: "b"}[0]

I can see that solving this might be tricky, but highly appreciated.

Afterthought: Seems like detecting item accessing can be done by the rule "if ['s first preceding non-blank character is present and is not operator". All other cases of [ is a list creation. However, capturing object which items are accessed is challenging with regular expressions.

echasnovski avatar Apr 18 '20 18:04 echasnovski

I can use a trick similar to the one for detecting function calls and kwargs when they aren't preceded by a plain function name. So I should be able to distinguish the array literals from item access that way in most cases.

vpetrovykh avatar May 01 '20 23:05 vpetrovykh