-
Notifications
You must be signed in to change notification settings - Fork 14
Tutorial: Add support for python packages
This example will create a FuzzyFilePath trigger to autocomplete Python package imports
Having a folder opened in Sublime, press alt+super+p
in an opened python file. At the bottom (status bar) you see the
current scope at your cursor. On an empty line it should print something like source.python. In
FuzzyFilePath Settings we create a new trigger by adding an object to "scopes" array and tell FuzzyFilePath to use
this trigger if the current scope contains .python. Remember to escape regex characters using double backslashes:
"scopes": [
{
"scope": "\\.python"
}
]
Using the force auto completion command, i.e. ctrl+shift+space
, will show all files within the current project that
have a valid file extensions. File extensions are searched within all triggers given in the scopes array. To restrict proposed paths to python files, add the attribute extensions
in the settings:
{
"scope": "\\.python",
"extensions": ["py"]
}
To enable auto completion, set attribute auto
to true:
{
"scope": "\\.python",
"extensions": ["py"],
"auto": true
}
This will always query filepaths while typing, thus we must restrict auto completions to a specific context.
If you are using Sublime build 3073 or later: on a line like
from FuzzyFilePath.project.ProjectManager import ProjectManager
, place the cursor on the import path and use the command ffp_show_context. To call the command, add{ "keys": ["alt+y"], "command": "ffp_show_context" }
to your keybindings. This will show the current context and its retrieved variables.
In this case the value prefix
is set to from:
{
"scope": "\\.python",
"extensions": ["py"],
"auto": true,
"prefix": ["from"]
}
Now filepaths are only proposed if the string from is before the current edited string. Filepaths are still inserted
relative, but we require absolute paths, thus set attribute relative
to false:
{
"scope": "\\.python",
"extensions": ["py"],
"auto": true,
"prefix": ["from"],
"relative": false
}
Inserting a file will print something like from /project/ProjectManager.py
. But our import should look like
from project.ProjectManager
. Using regex tupels, the path maybe cleaned up after insertion:
{
"scope": "\\.python",
"extensions": ["py"],
"auto": true,
"prefix": ["from"],
"relative": false,
"replace_on_insert": [
["^\\/", ""], // remove first slash
["\\/", "."], // replace slashed by dots
["\\.py$", ""], // remove file extension
]
}
This will insert the above path as from project.ProjectManager
. In case of Sublime Text Plugins, packages start with
its name, like FuzzyFilePath
. Adding this trigger to project settings and adjusting the replacements to:
{
"scope": "\\.python",
"extensions": ["py"],
"auto": true,
"prefix": ["from"],
"relative": false,
"replace_on_insert": [
["^\\/", ""], // remove first slash
["\\/", "."], // replace slashed by dots
["^(.*)\\.py$", "FuzzyFilePath.\\1"], // add package file
]
}
will insert local paths only and prefix them with the project's package name, i.e.
from FuzzyFilePath.project.ProjectManager