Mercurial > vim
comparison runtime/doc/vim9.txt @ 26980:8796f1384750 v8.2.4019
patch 8.2.4019: Vim9: import mechanism is too complicated
Commit: https://github.com/vim/vim/commit/d5f400c607182db6d4fbe2964471d796277f67e8
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Jan 6 21:10:28 2022 +0000
patch 8.2.4019: Vim9: import mechanism is too complicated
Problem: Vim9: import mechanism is too complicated.
Solution: Do not use the Javascript mechanism but a much simpler one.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 06 Jan 2022 22:15:04 +0100 |
parents | be85735650f7 |
children | 3e661b0cf500 |
comparison
equal
deleted
inserted
replaced
26979:2fb4968983af | 26980:8796f1384750 |
---|---|
1430 `:export` can only be used in Vim9 script, at the script level. | 1430 `:export` can only be used in Vim9 script, at the script level. |
1431 | 1431 |
1432 | 1432 |
1433 Import ~ | 1433 Import ~ |
1434 *:import* *:imp* *E1094* | 1434 *:import* *:imp* *E1094* |
1435 The exported items can be imported individually in another Vim9 script: > | 1435 The exported items can be imported in another Vim9 script: > |
1436 import EXPORTED_CONST from "thatscript.vim" | 1436 import "myscript.vim" |
1437 import MyClass from "myclass.vim" | 1437 |
1438 | 1438 This makes each item available as "myscript.item". |
1439 To import multiple items at the same time: > | 1439 |
1440 import {someValue, MyClass} from "thatscript.vim" | 1440 In case the name is long or ambiguous, another name can be specified: > |
1441 | 1441 import "thatscript.vim" as That |
1442 In case the name is ambiguous, another name can be specified: > | |
1443 import MyClass as ThatClass from "myclass.vim" | |
1444 import {someValue, MyClass as ThatClass} from "myclass.vim" | |
1445 | |
1446 To import all exported items under a specific identifier: > | |
1447 import * as That from 'thatscript.vim' | |
1448 | 1442 |
1449 Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free | 1443 Then you can use "That.EXPORTED_CONST", "That.someValue", etc. You are free |
1450 to choose the name "That", but it is highly recommended to use the name of the | 1444 to choose the name "That". Use something that will be recognized as referring |
1451 script file to avoid confusion. Also avoid command names, because the name | 1445 to the imported script. Avoid command names, because the name will shadow |
1452 will shadow them. | 1446 them. |
1447 | |
1448 In case the dot in the name is unwanted, a local reference can be made: > | |
1449 var ThatFunc = That.LongFuncName | |
1450 | |
1451 This also works for constants: > | |
1452 cost MAXLEN = That.MAX_LEN_OF_NAME | |
1453 | |
1454 This does not work for variables, you could use a setter function and make a | |
1455 local reference for it. | |
1453 | 1456 |
1454 `:import` can also be used in legacy Vim script. The imported items still | 1457 `:import` can also be used in legacy Vim script. The imported items still |
1455 become script-local, even when the "s:" prefix is not given. | 1458 become script-local, even when the "s:" prefix is not given. |
1456 | 1459 |
1457 `:import` can not be used in a function. Imported items are intended to exist | 1460 `:import` can not be used in a function. Imported items are intended to exist |
1468 longer and unique, to avoid loading the wrong file. | 1471 longer and unique, to avoid loading the wrong file. |
1469 Note that "after/import" is not used. | 1472 Note that "after/import" is not used. |
1470 | 1473 |
1471 Once a vim9 script file has been imported, the result is cached and used the | 1474 Once a vim9 script file has been imported, the result is cached and used the |
1472 next time the same script is imported. It will not be read again. | 1475 next time the same script is imported. It will not be read again. |
1476 | |
1477 It is not allowed to import the same script twice, also when using two | |
1478 different "as" names. | |
1473 *:import-cycle* | 1479 *:import-cycle* |
1474 The `import` commands are executed when encountered. If that script (directly | 1480 The `import` commands are executed when encountered. If that script (directly |
1475 or indirectly) imports the current script, then items defined after the | 1481 or indirectly) imports the current script, then items defined after the |
1476 `import` won't be processed yet. Therefore cyclic imports can exist, but may | 1482 `import` won't be processed yet. Therefore cyclic imports can exist, but may |
1477 result in undefined items. | 1483 result in undefined items. |
1489 < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. | 1495 < This goes in .../plugin/anyname.vim. "anyname.vim" can be freely chosen. |
1490 | 1496 |
1491 2. In the autoload script do the actual work. You can import items from | 1497 2. In the autoload script do the actual work. You can import items from |
1492 other files to split up functionality in appropriate pieces. > | 1498 other files to split up functionality in appropriate pieces. > |
1493 vim9script | 1499 vim9script |
1494 import FilterFunc from "../import/someother.vim" | 1500 import "../import/someother.vim" as other |
1495 def searchfor#Stuff(arg: string) | 1501 def searchfor#Stuff(arg: string) |
1496 var filtered = FilterFunc(arg) | 1502 var filtered = other.FilterFunc(arg) |
1497 ... | 1503 ... |
1498 < This goes in .../autoload/searchfor.vim. "searchfor" in the file name | 1504 < This goes in .../autoload/searchfor.vim. "searchfor" in the file name |
1499 must be exactly the same as the prefix for the function name, that is how | 1505 must be exactly the same as the prefix for the function name, that is how |
1500 Vim finds the file. | 1506 Vim finds the file. |
1501 | 1507 |