Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: VSCode/(neo)vim Autocomplete #24

Merged
merged 17 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = {
'testem.js',
'blueprints/*/index.js',
'config/**/*.js',
'tests/dummy/config/**/*.js'
'tests/dummy/config/**/*.js',
'lib/**/*.js'
],
excludedFiles: [
'addon/**',
Expand All @@ -37,7 +38,7 @@ module.exports = {
],
parserOptions: {
sourceType: 'script',
ecmaVersion: 2015
ecmaVersion: 2018
},
env: {
browser: false,
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ In other words, it's **pods + angle bracket component = goodness**.
* More concise component invocation while making it explicit where it comes from
* No hyphens needed!
* Relative imports!
* Autocomplete imported components with [Unstable Ember Language Server](unstable-ls)!

Can be used together with
* [helper imports](https://github.com/patricklx/ember-template-helper-imports)
Expand Down Expand Up @@ -73,6 +74,19 @@ import { BasicDropdown as SameDropdown } from 'ember-basic-dropdown/components';
<SameDropdown />
```

Editor Integration
------------------------------------------------------------------------------

Editor integration currently provides auto completion of imported components.

### VSCode

Currently works with **VSCode** combined with the [Unstable Ember Language Server](unstable-ls).

### (Neo)Vim

Possible by using https://github.com/NullVoxPopuli/coc-ember


Motivation
------------------------------------------------------------------------------
Expand Down Expand Up @@ -109,3 +123,5 @@ License
------------------------------------------------------------------------------

This project is licensed under the [MIT License](LICENSE.md).

unstable-ls: https://marketplace.visualstudio.com/items?itemName=lifeart.vscode-ember-unstable
57 changes: 57 additions & 0 deletions lib/langserver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const { URI } = require('vscode-uri');
const { hasOctaneImports, transformOctaneImports } = require('./octane-utils');
const { transformImports } = require('./utils');

async function onComplete(projectRoot, opts) {
function log() {
return opts.server.connection.console.log(...arguments);
}

if (opts.focusPath.node.type === 'ElementNode') {
const content = opts.textDocument.getText();

if (content) {
let mightHaveImports = content.includes('import');
if (!mightHaveImports) {
return [];
}
let filePath = uriToPath(opts.textDocument.uri);
let isOctane = hasOctaneImports(content);
let componentImports;

if (isOctane) {
let legacyContent = transformOctaneImports(content, filePath);
componentImports = transformImports(legacyContent, filePath, projectRoot);
} else {
componentImports = transformImports(content, filePath, projectRoot);
}

if (componentImports && componentImports.imports) {
log(componentImports.imports);
return componentImports.imports.reduce((all, imp) => {
if (imp.isLocalNameValid) {
all.push({
kind: 7,
label: imp.localName,
detail: `Imported from '${imp.importPath}'`,
sortText: `0-${imp.localName}-${imp.importPath}`
});
}
return all;
}, []);
}
}
}

return [];
}

module.exports.onComplete = onComplete;

function uriToPath(stringUri) {
const uri = URI.parse(stringUri);
if (uri.scheme !== 'file') {
return undefined;
}
return uri.fsPath;
}
1 change: 1 addition & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function getModuleRelatedImportPath(root, importPath) {
.join("/");
}


function transformImports(contents, relativePath, root, usingStylesImport) {
let imports = [];
let rewrittenContents = contents.replace(
Expand Down
12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"dependencies": {
"broccoli-persistent-filter": "^2.1.1",
"ember-cli-babel": "^7.1.2",
"md5-hex": "^2.0.0"
"md5-hex": "^2.0.0",
"vscode-uri": "^2.1.1"
},
"devDependencies": {
"@commitlint/cli": "^7.2.1",
Expand Down Expand Up @@ -68,12 +69,19 @@
"semantic-release": "^15.13.2"
},
"engines": {
"node": "6.* || 8.* || >= 10.*"
"node": ">= 10.1 || 11.* || 12.*"
},
"ember-addon": {
"configPath": "tests/dummy/config",
"before": [
"ember-cli-htmlbars"
]
},
"ember-language-server": {
"entry": "./lib/langserver.js",
"version": 1,
"capabilities": {
"completionProvider": true
}
}
}
5 changes: 4 additions & 1 deletion tests/dummy/app/pods/js-imports/template.hbs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Button } from 'ui';
import { Button as ButtonTwo } from 'ui';
import { Input } from './nested';
import InputOther from './nested/input';
--- hbs ---

<Button>I'm a globally referenced button</Button>

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12233,6 +12233,11 @@ verror@1.10.0:
core-util-is "1.0.2"
extsprintf "^1.2.0"

vscode-uri@^2.1.1:
version "2.1.1"
resolved "https://registry.yarnpkg.com/vscode-uri/-/vscode-uri-2.1.1.tgz#5aa1803391b6ebdd17d047f51365cf62c38f6e90"
integrity sha512-eY9jmGoEnVf8VE8xr5znSah7Qt1P/xsCdErz+g8HYZtJ7bZqKH5E3d+6oVNm1AC/c6IHUDokbmVXKOi4qPAC9A==

w3c-hr-time@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/w3c-hr-time/-/w3c-hr-time-1.0.1.tgz#82ac2bff63d950ea9e3189a58a65625fedf19045"
Expand Down