├── .gitignore ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── lib ├── api.js ├── helpers.js └── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | node_modules 3 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | #### 1.0.3 2 | 3 | - Add docs. Publishing a version was required to update them on the atom.io/packages page as well. 4 | 5 | #### 1.0.2 6 | 7 | - Make package lightweight by removing unnecessary dependency 8 | 9 | #### 1.0.1 10 | 11 | - Handle readdir errors gracefully 12 | 13 | #### 1.0.0 14 | 15 | - Initial release 16 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 steelbrain 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | AutoComplete-Swift 2 | ================== 3 | 4 | Autocomplete package for Swift using [`SourceKittenDaemon`][]. Made possible by the testing done by [@memorion](https://github.com/memorion). 5 | 6 | ## Installation 7 | 8 | To use this package, You must have [`SourceKittenDaemon`][] installed. Please note it is different from `SourceKitten`. 9 | 10 | After you have installed it, you can install this package either from the settings view or CLI with 11 | 12 | ``` 13 | apm install autocomplete-swift 14 | ``` 15 | 16 | ## Usage Instructions 17 | 18 | Make sure you have the bin of SourceKittenDaemon in your $PATH. If you changed your PATH recently, make sure to restart Atom or restart the terminal and start Atom from that terminal. 19 | 20 | ## Debugging Instructions 21 | 22 | If you are having trouble using this package or don't see any errors, check Atom's developer console for any messages. You can toggle it by invoking `Window: Toggle Dev Tools` from the command pellete or View->Developer->Toggle Developer Tools from the top bar. 23 | 24 | 25 | ## License 26 | 27 | MIT License © steelbrain 28 | 29 | [`SourceKittenDaemon`]:https://github.com/terhechte/SourceKittenDaemon#building--installation 30 | -------------------------------------------------------------------------------- /lib/api.js: -------------------------------------------------------------------------------- 1 | 'use babel' 2 | 3 | import {request} from 'http' 4 | import {BufferedProcess} from 'atom' 5 | import {getEnv, tempFile} from './helpers' 6 | import {basename} from 'path' 7 | 8 | export class API { 9 | constructor(configPath) { 10 | this.port = 44877 11 | this.process = new BufferedProcess({ 12 | command: 'SourceKittenDaemon', 13 | args: ['start', '--port', this.port, '--project', configPath], 14 | options: {env: getEnv()}, 15 | stdout: function(buffer) { 16 | console.debug('KittenDaemon :: stdout', buffer.toString()) 17 | }, 18 | stderr: function(buffer) { 19 | console.debug('KittenDaemon :: stderr', buffer.toString()) 20 | } 21 | }) 22 | } 23 | autocomplete(filePath, fileContents, characterIndex) { 24 | const name = basename(filePath) 25 | const port = this.port 26 | return tempFile(name, fileContents, function(tempFile) { 27 | return new Promise(function(resolve, reject) { 28 | const data = [] 29 | const req = request({ 30 | hostname: '127.0.0.1', 31 | port: port, 32 | path: '/complete', 33 | method: 'GET', 34 | headers: { 35 | 'X-Offset': characterIndex, 36 | 'X-Path': tempFile, 37 | 'X-File': name 38 | } 39 | }, function(res) { 40 | res.on('error', reject) 41 | res.on('data', function(chunk) { 42 | data.push(chunk) 43 | }) 44 | res.on('end', function() { 45 | resolve(JSON.parse(data.join(''))) 46 | }) 47 | }) 48 | req.end() 49 | }) 50 | }) 51 | } 52 | dispose() { 53 | this.process.kill() 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /lib/helpers.js: -------------------------------------------------------------------------------- 1 | 'use babel' 2 | 3 | import getEnvironment from 'consistent-env' 4 | export {tempFile} from 'atom-linter' 5 | export function getEnv() { 6 | return getEnvironment() 7 | } 8 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use babel' 2 | 3 | import {CompositeDisposable, Disposable} from 'atom' 4 | import {readdir} from 'fs' 5 | import {join as joinPath} from 'path' 6 | import {API} from './api' 7 | 8 | module.exports = { 9 | activate: function() { 10 | this.subscriptions = new CompositeDisposable() 11 | this.api = null 12 | 13 | const paths = atom.project.getPaths() 14 | if (paths.length) { 15 | const rootDirectory = paths[0] 16 | readdir(rootDirectory, (error, files) => { 17 | if (error) { 18 | atom.notifications.addError('[autocomplete-swift] Error scanning project root for files, see log for more info') 19 | console.log('autocomplete-swift scan error', error) 20 | return 21 | } 22 | const filesLength = files.length 23 | for (let i = 0; i < filesLength; ++i) { 24 | const file = files[i] 25 | if (file.endsWith('.xcodeproj')) { 26 | const filePath = joinPath(rootDirectory, file) 27 | this.api = new API(filePath) 28 | this.subscriptions.add(this.api) 29 | } 30 | } 31 | }) 32 | } 33 | }, 34 | provideAC: function() { 35 | return { 36 | selector: '.source.swift', 37 | disableForSelector: '.comment', 38 | getSuggestions: ({editor, bufferPosition, prefix}) => { 39 | const replacementPrefix = prefix.substr(0, 1) === '.' ? prefix.substr(1) : prefix 40 | if (this.api === null) { 41 | return [] 42 | } 43 | const characterIndex = editor.getBuffer().characterIndexForPosition(bufferPosition) 44 | const fileContents = editor.getText() 45 | const filePath = editor.getPath() 46 | return this.api.autocomplete(filePath, fileContents, characterIndex).then(function(results) { 47 | return results.map(function(result) { 48 | return { 49 | text: result.name, 50 | replacementPrefix: replacementPrefix, 51 | displayText: result.descriptionKey, 52 | type: result.typeName, 53 | leftLabel: result.typeName 54 | } 55 | }) 56 | }) 57 | 58 | } 59 | } 60 | }, 61 | deactivate: function() { 62 | this.subscriptions.dispose() 63 | this.api = null 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "autocomplete-swift", 3 | "version": "1.0.2", 4 | "description": "Autocomplete provider for swift", 5 | "main": "lib/index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/steelbrain/autocomplete-swift.git" 12 | }, 13 | "author": "steelbrain", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/steelbrain/autocomplete-swift/issues" 17 | }, 18 | "homepage": "https://github.com/steelbrain/autocomplete-swift#readme", 19 | "providedServices": { 20 | "autocomplete.provider": { 21 | "versions": { 22 | "2.0.0": "provideAC" 23 | } 24 | } 25 | }, 26 | "dependencies": { 27 | "atom-linter": "^4.3.0", 28 | "consistent-env": "^1.0.0" 29 | } 30 | } 31 | --------------------------------------------------------------------------------