tomblachut / svelte-intellij

Svelte components in WebStorm and friends
MIT License
482 stars 38 forks source link

Import inserted in the template rather than in the script #129

Closed unlocomqx closed 3 years ago

unlocomqx commented 4 years ago

This is a tricky one An auto-import is inserted in the template, it should be inserted in the script section 2020-08-01_10-36

tomblachut commented 4 years ago

I have somewhat working solution on local branch but JB refactored this inspection in 2020.2 and I'm unsure how to proceed for now

native importing code: Old: com.intellij.lang.javascript.modules.ES6ModulesDependenciesInspection (id ES6ModulesDependencies) New: com.intellij.lang.javascript.inspections.JSUnresolvedVariableInspection (id JSUnresolvedVariable) com.intellij.lang.javascript.inspections.JSUnresolvedFunctionInspection (id JSUnresolvedFunction)

Relevant, also new: com.intellij.lang.javascript.validation.JSUnresolvedReferenceErrorUpdater

Both old and new inspections use different classes and it's impossible to support 2020.1 and 2020.2 with one binary

unlocomqx commented 4 years ago

I don't even know if inspections are relevant to this issue, it's an auto-import so it's probably relevant to completion rather than inspection. Maybe I don't understand but here's a gif to alleviate any potential ambiguity

Peek 2020-08-02 10-16

unlocomqx commented 4 years ago

The shortest way I think is to change the behaviour of ES6ImportPsiUtil::findAnchorToInsert

2020-08-02_10-24

findAnchorToInsert:236, ES6ImportPsiUtil (com.intellij.lang.ecmascript6.psi.impl)
findPlaceForImport:204, ES6ImportPsiUtil (com.intellij.lang.ecmascript6.psi.impl)
findPlaceByModuleName:174, ES6ImportPsiUtil (com.intellij.lang.ecmascript6.psi.impl)
findPlaceAndInsertES6Import:460, ES6CreateImportUtil (com.intellij.lang.ecmascript6.psi.impl)
createImportOrUseExisting:302, ES6AddImportExecutor (com.intellij.lang.ecmascript6.actions)
executeImpl:46, ImportES6ModuleFix (com.intellij.lang.javascript.modules)
invokeAction:104, ImportCommonJSModuleFix (com.intellij.lang.javascript.modules)
insertJSImport:730, ES6ImportPsiUtil (com.intellij.lang.ecmascript6.psi.impl)
insertJSImport:717, ES6ImportPsiUtil (com.intellij.lang.ecmascript6.psi.impl)
insertJSImport:680, ES6ImportPsiUtil (com.intellij.lang.ecmascript6.psi.impl)
insertJSImport:672, ES6ImportPsiUtil (com.intellij.lang.ecmascript6.psi.impl)
......
insertLookupItem:519, JSImportCompletionUtil (com.intellij.lang.javascript.completion)
importForElement:451, JSImportCompletionUtil (com.intellij.lang.javascript.completion)
access$100:63, JSImportCompletionUtil (com.intellij.lang.javascript.completion)
handleInsert:543, JSImportCompletionUtil$ImportInsertHandler (com.intellij.lang.javascript.completion)
....
lambda$insertItem$15:688, CodeCompletionHandlerBase (com.intellij.codeInsight.completion)
...
doInsertItem:587, CodeCompletionHandlerBase (com.intellij.codeInsight.completion)
insertItemHonorBlockSelection:547, CodeCompletionHandlerBase (com.intellij.codeInsight.completion)
lookupItemSelected:493, CodeCompletionHandlerBase (com.intellij.codeInsight.completion)
...
bbuhler commented 3 years ago

It's the same for the quick action "insert import":

Screen record from 2020-10-30 14 54 26

Treverix commented 3 years ago

This works for me (now) unless I missed something obvious

svelte-import

bbuhler commented 3 years ago

@Treverix it does not work outside of the script tag.

Treverix commented 3 years ago

@bbuhler ok, I see - I just noticed that it works inside the script tag and also on a blank file now - it does not put the import inside the curly brackets (anymore) but yes, if there is script element, it does not add it to it, the import is put outside.

unlocomqx commented 3 years ago

Thank you @tomblachut for your work on the plugin and the platform!

tomblachut commented 3 years ago

It's a pleasure 😊