Advertisement
Guest User

init.coffee

a guest
Sep 13th, 2016
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {CompositeDisposable} = require 'atom'
  2. path = require 'path'
  3.  
  4. module.exports =
  5.   config:
  6.     lineLength:
  7.       type: 'integer'
  8.       default: '80'
  9.     filters:
  10.       type: 'string'
  11.       default: ''
  12.     extensions:
  13.       type: 'string'
  14.       default: 'c++,cc,cpp,cu,cuh,h,hpp'
  15.     executablePath:
  16.       type: 'string'
  17.       default: path.join __dirname, '..', 'bin', 'cpplint.py'
  18.  
  19.   activate: ->
  20.     require('atom-package-deps').install()
  21.     @subscriptions = new CompositeDisposable
  22.  
  23.     @subscriptions.add atom.config.observe 'linter-cpplint.executablePath',
  24.     (executablePath) =>
  25.       @cpplintPath = executablePath
  26.  
  27.     @subscriptions.add atom.config.observe 'linter-cpplint.lineLength', =>
  28.       @updateParameters()
  29.  
  30.     @subscriptions.add atom.config.observe 'linter-cpplint.filters', =>
  31.       @updateParameters()
  32.  
  33.     @subscriptions.add atom.config.observe 'linter-cpplint.extensions', =>
  34.       @updateParameters()
  35.  
  36.   deactivate: ->
  37.     @subscriptions.dispose()
  38.  
  39.   provideLinter: ->
  40.     helpers = require('atom-linter')
  41.     provider =
  42.       name: 'cpplint'
  43.       grammarScopes: ['source.cpp']
  44.       scope: 'file'
  45.       # cpplint only lint file(s).
  46.       lintOnFly: false
  47.       lint: (textEditor) =>
  48.         filePath = textEditor.getPath()
  49.         parameters = @parameters.slice()
  50.  
  51.         # File path is the last parameter.
  52.         parameters.push(filePath)
  53.  
  54.         return helpers
  55.             .exec('/usr/bin/python2', parameters, stream: 'stderr').then (result) ->
  56.           toReturn = []
  57.           regex = /.+:(\d+):(.+)\[\d+\]/g
  58.  
  59.           while (match = regex.exec(result)) isnt null
  60.             line = parseInt(match[1]) or 1
  61.             message = match[2]
  62.  
  63.             # cpplint line is 1-based. Line 0 is for copyright and header_guard.
  64.             line = Math.max(0, line - 1)
  65.  
  66.             range = [
  67.               [line, 0]
  68.               [line, textEditor.getBuffer().lineLengthForRow(line)]
  69.             ]
  70.  
  71.             toReturn.push({
  72.               type: 'Warning'
  73.               text: message
  74.               filePath: filePath
  75.               range: range
  76.             })
  77.           return toReturn
  78.  
  79.   updateParameters: ->
  80.     lineLength = atom.config.get 'linter-cpplint.lineLength'
  81.     filters = atom.config.get 'linter-cpplint.filters'
  82.     extensions = atom.config.get 'linter-cpplint.extensions'
  83.     parameters = []
  84.     parameters.push(@cpplintPath)
  85.     if lineLength
  86.       parameters.push('--linelength', lineLength)
  87.     if filters
  88.       parameters.push('--filter', filters)
  89.     if extensions
  90.       parameters.push('--extensions', extensions)
  91.     @parameters = parameters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement