Repository navigation
Expand file tree
/
Copy pathcode-context.coffee
More file actions
69 lines (59 loc) · 2.09 KB
/
Copy pathcode-context.coffee
File metadata and controls
69 lines (59 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module.exports =
class CodeContext
filename: null
filepath: null
lineNumber: null
shebang: null
textSource: null
# Public: Initializes a new {CodeContext} object for the given file/line
#
# @filename - The {String} filename of the file to execute.
# @filepath - The {String} path of the file to execute.
# @textSource - The {String} text to under "Selection Based". (default: null)
#
# Returns a newly created {CodeContext} object.
constructor: (@filename, @filepath, @textSource = null) ->
# Public: Creates a {String} representation of the file and line number
#
# fullPath - Whether to expand the file path. (default: true)
#
# Returns the "file colon line" {String}.
fileColonLine: (fullPath = true) ->
if fullPath
fileColonLine = @filepath
else
fileColonLine = @filename
return fileColonLine unless @lineNumber
"#{fileColonLine}:#{@lineNumber}"
# Public: Retrieves the text from whatever source was given on initialization
#
# prependNewlines - Whether to prepend @lineNumber newlines (default: true)
#
# Returns the code selection {String}
getCode: (prependNewlines = true) ->
code = @textSource?.getText()
return code unless prependNewlines and @lineNumber
newlineCount = Number(@lineNumber)
newlines = Array(newlineCount).join("\n")
"#{newlines}#{code}"
# Public: Retrieves the command name from @shebang
#
# Returns the {String} name of the command or {undefined} if not applicable.
shebangCommand: ->
sections = @shebangSections()
return unless sections
sections[0]
# Public: Retrieves the command arguments (such as flags or arguments to
# /usr/bin/env) from @shebang
#
# Returns the {String} name of the command or {undefined} if not applicable.
shebangCommandArgs: ->
sections = @shebangSections()
return [] unless sections
sections[1..sections.length-1]
# Public: Splits the shebang string by spaces to extra the command and
# arguments
#
# Returns the {String} name of the command or {undefined} if not applicable.
shebangSections: ->
@shebang?.split(' ')