Skip to content

Commit 90728ba

Browse files
committed
Initial commit
0 parents  commit 90728ba

File tree

8 files changed

+187
-0
lines changed

8 files changed

+187
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
npm-debug.log
3+
node_modules

CHANGELOG.md

Whitespace-only changes.

LICENSE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2014 FILSH Media GmbH
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# autocomplete+ path autocompletion
2+
3+
[View the changelog](https://github.com/saschagehlich/autocomplete-paths/blob/master/CHANGELOG.md)
4+
5+
Adds path autocompletion to autocomplete+
6+
7+
![autocomplete-paths]()
8+
9+
## Installation
10+
11+
You can install autocomplete-paths using the Preferences pane.
12+
13+
**Please make sure you have autocomplete-plus installed as well**

lib/autocomplete-paths.coffee

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
_ = require "underscore-plus"
2+
PathsProvider = require "./paths-provider"
3+
4+
module.exports =
5+
editorSubscription: null
6+
providers: []
7+
autocomplete: null
8+
9+
###
10+
* Registers a SnippetProvider for each editor view
11+
###
12+
activate: ->
13+
atom.packages.activatePackage("autocomplete-plus")
14+
.then (pkg) =>
15+
@autocomplete = pkg.mainModule
16+
@registerProviders()
17+
18+
###
19+
* Registers a SnippetProvider for each editor view
20+
###
21+
registerProviders: ->
22+
@editorSubscription = atom.workspaceView.eachEditorView (editorView) =>
23+
if editorView.attached and not editorView.mini
24+
provider = new PathsProvider editorView
25+
26+
@autocomplete.registerProviderForEditorView provider, editorView
27+
28+
@providers.push provider
29+
30+
###
31+
* Cleans everything up, unregisters all SnippetProvider instances
32+
###
33+
deactivate: ->
34+
@editorSubscription?.off()
35+
@editorSubscription = null
36+
37+
@providers.forEach (provider) =>
38+
@autocomplete.unregisterProvider provider
39+
40+
@providers = []

lib/paths-provider.coffee

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{Range} = require "atom"
2+
{Provider, Suggestion} = require "autocomplete-plus"
3+
fuzzaldrin = require "fuzzaldrin"
4+
_ = require "underscore-plus"
5+
path = require "path"
6+
fs = require "fs"
7+
8+
module.exports =
9+
class PathsProvider extends Provider
10+
wordRegex: /(\.+)?\/([a-zA-Z0-9\.\/_-]+)?/g
11+
exclusive: true
12+
buildSuggestions: ->
13+
selection = @editor.getSelection()
14+
prefix = @prefixOfSelection selection
15+
return unless prefix.length
16+
17+
suggestions = @findSuggestionsForPrefix prefix
18+
return unless suggestions.length
19+
return suggestions
20+
21+
findSuggestionsForPrefix: (prefix) ->
22+
basePath = path.dirname @editor.getPath()
23+
24+
prefixFileName = path.basename prefix
25+
prefixBasePath = path.dirname prefix
26+
27+
# path.basename returns the last portion of a path, but
28+
# never "nothing" (in case of a trailing slash)
29+
# In this case, add the filename to the path and let
30+
# the filename be empty
31+
if prefix.match /[\/|\\]$/
32+
prefixBasePath = path.join prefixBasePath, prefixFileName
33+
prefixFileName = ""
34+
35+
prefixPath = path.resolve basePath, prefixBasePath
36+
37+
files = fs.readdirSync prefixPath
38+
results = fuzzaldrin.filter files, prefixFileName
39+
40+
suggestions = for result in results
41+
filePath = path.resolve basePath, prefixPath, result
42+
stat = fs.statSync filePath
43+
if stat.isDirectory()
44+
label = "Dir"
45+
result += "/"
46+
else
47+
label = "File"
48+
49+
# If base path starts with a ., add
50+
# another slash to the result path
51+
resultPath = prefixBasePath
52+
unless resultPath.match /[\/|\\]$/
53+
resultPath += path.sep
54+
55+
new Suggestion this,
56+
word: result
57+
prefix: prefix
58+
label: label
59+
data:
60+
body: resultPath + result
61+
62+
return suggestions
63+
64+
confirm: (suggestion) ->
65+
selection = @editor.getSelection()
66+
startPosition = selection.getBufferRange().start
67+
buffer = @editor.getBuffer()
68+
69+
# Replace the prefix with the body
70+
cursorPosition = @editor.getCursorBufferPosition()
71+
buffer.delete Range.fromPointWithDelta(cursorPosition, 0, -suggestion.prefix.length)
72+
@editor.insertText suggestion.data.body
73+
74+
# Move the cursor behind the body
75+
suffixLength = suggestion.data.body.length - suggestion.prefix.length
76+
@editor.setSelectedBufferRange [startPosition, [startPosition.row, startPosition.column + suffixLength]]
77+
78+
setTimeout(=>
79+
@editorView.trigger "autocomplete-plus:activate"
80+
, 100)
81+
82+
return false # Don't fall back to the default behavior

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "autocomplete-paths",
3+
"main": "./lib/autocomplete-paths",
4+
"version": "0.0.0",
5+
"description": "Adds path autocompletion to autocomplete+",
6+
"repository": "https://github.com/saschagehlich/autocomplete-paths",
7+
"license": "MIT",
8+
"engines": {
9+
"atom": ">0.50.0"
10+
},
11+
"dependencies": {
12+
"underscore-plus": "~1.1.2",
13+
"autocomplete-plus": "git://github.com/saschagehlich/autocomplete-plus.git",
14+
"fuzzaldrin": "~1.0.0"
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
AutocompleteSnippets = require "../lib/autocomplete-snippets"
2+
3+
# Use the command `window:run-package-specs` (cmd-alt-ctrl-p) to run specs.
4+
#
5+
# To run a specific `it` or `describe` block add an `f` to the front (e.g. `fit`
6+
# or `fdescribe`). Remove the `f` to unfocus the block.
7+
8+
describe "AutocompleteSnippets", ->
9+
activationPromise = null
10+
11+
beforeEach ->
12+
atom.workspaceView = new WorkspaceView
13+
activationPromise = atom.packages.activatePackage "autocomplete-snippets"

0 commit comments

Comments
 (0)