RobertOstermann / vscode-inline-parameters

An Visual Studio Code extension that adds inline parameter annotations.
https://marketplace.visualstudio.com/items?itemName=RobertOstermann.inline-parameters-extended
MIT License
29 stars 6 forks source link

Fix incorrect inline parameter names when complex type hinting is used #17

Closed iAmNikola closed 1 year ago

iAmNikola commented 1 year ago

Hi :) Thanks for the great project!

I noticed that inline parameteres for python don't work as intended when complex type hinting is used (see picture.) 2023-09-11_11-16-39

This PR hopefully fixes that. I tested the code logic locally and it works on various test examples that I came up with. I don't know how to set it up to run it in VS Code.

This is the code i used to test the new logic locally:

const definition = `
    first: str,
    second: str | Tuple[int, int, float, float, float],
    third: Unknown,
    fourth: dict[Unknown, Unknown],
    *fifth: Unknown,
    sixth: Unknown | None = None`

let isVariadic = false;
const pythonParameterRegex = /(\*?[a-zA-Z_][0-9a-zA-Z_]+?):/g;
const parameters = Array.from(definition.match(pythonParameterRegex), m => m)
      // eslint-disable-next-line no-useless-escape
      .map(parameter => {
        const paramaterName = parameter.replace(/\*|:/g, "");
        if (isVariadic) {
          return null;
        }
        if (parameter.includes("*")) isVariadic = true;

        return paramaterName;
      })
      .filter(parameter => parameter);

console.log(parameters)

Let me know if you think any additional changes are needed.

RobertOstermann commented 1 year ago

I think these changes will work. Thanks for the contributions!