postcss-selector-parser icon indicating copy to clipboard operation
postcss-selector-parser copied to clipboard

Would we add `pos` to ast source property

Open HairyRabbit opened this issue 3 years ago • 7 comments

Hi, I write a vsc extension want to implement go to definition, it require position info. I see that token already has pos property, would we also add to node.source filed?

HairyRabbit avatar Dec 14 '21 01:12 HairyRabbit

Why you need this on mode.source property? Can you show AST which you expected?

alexander-akait avatar Dec 14 '21 10:12 alexander-akait

I write css code in js via template string, like

const style = css`
.foo { color: red; }
`

style.foo
     //  ^---- click foo will jump to `.foo` selector

the tss.getDefinitionAndBoundSpan require position info,

interface LanguageService {
  getDefinitionAtPosition(fileName: string, position: number): readonly DefinitionInfo[] | undefined;
  getDefinitionAndBoundSpan(fileName: string, position: number): DefinitionInfoAndBoundSpan | undefined;
}

I need comput position base on template string node, css rule and selector. So I hope selector parser can provide offset just like postcss:

{
  source: {
    start: { offset: 0, line: 1, column: 1 }
  }
}

HairyRabbit avatar Dec 14 '21 12:12 HairyRabbit

But you can calculate them

alexander-akait avatar Dec 14 '21 12:12 alexander-akait

sure, but I think it can implement when parse, or any api provide like ts.getPositionFromLineAndColumn

HairyRabbit avatar Dec 14 '21 13:12 HairyRabbit

hm, do you need offset, because we have https://github.com/postcss/postcss-selector-parser/blob/master/src/parser.js#L49

alexander-akait avatar Dec 14 '21 13:12 alexander-akait

yes, I fork the code then add TOKEN.START_POS to getSource function and do some update with getTokenSource, looks fine for me.

HairyRabbit avatar Dec 14 '21 14:12 HairyRabbit

PR welcome

alexander-akait avatar Dec 14 '21 14:12 alexander-akait