Class: Solargraph::Workspace

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/workspace.rb,
lib/solargraph/workspace/config.rb

Overview

A workspace consists of the files in a project's directory and the project's configuration. It provides a Source for each file to be used in an associated Library or ApiMap.

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory = '', config = nil) ⇒ Workspace

Returns a new instance of Workspace.

Parameters:

  • directory (String) (defaults to: '')
  • config (Config, nil) (defaults to: nil)


29
30
31
32
33
34
35
36
# File 'lib/solargraph/workspace.rb', line 29

def initialize directory = '', config = nil
  @directory = directory
  @config = config
  load_sources
  @gemnames = []
  @require_paths = generate_require_paths
  require_plugins
end

Instance Attribute Details

#directoryString (readonly)

Returns:

  • (String)


16
17
18
# File 'lib/solargraph/workspace.rb', line 16

def directory
  @directory
end

#gemnamesArray<String> (readonly) Also known as: source_gems

Returns:



24
25
26
# File 'lib/solargraph/workspace.rb', line 24

def gemnames
  @gemnames
end

#require_pathsArray<String> (readonly)

The require paths associated with the workspace.

Returns:



21
22
23
# File 'lib/solargraph/workspace.rb', line 21

def require_paths
  @require_paths
end

Instance Method Details

#configSolargraph::Workspace::Config



39
40
41
# File 'lib/solargraph/workspace.rb', line 39

def config
  @config ||= Solargraph::Workspace::Config.new(directory)
end

#filenamesArray<String>

Returns:



81
82
83
# File 'lib/solargraph/workspace.rb', line 81

def filenames
  source_hash.keys
end

#gemspec?Boolean

True if the workspace contains at least one gemspec file.

Returns:

  • (Boolean)


118
119
120
# File 'lib/solargraph/workspace.rb', line 118

def gemspec?
  !gemspecs.empty?
end

#gemspecsArray<String>

Get an array of all gemspec files in the workspace.

Returns:



125
126
127
128
129
130
# File 'lib/solargraph/workspace.rb', line 125

def gemspecs
  return [] if directory.empty? || directory == '*'
  @gemspecs ||= Dir[File.join(directory, '**/*.gemspec')].select do |gs|
    config.allow? gs
  end
end

#has_file?(filename) ⇒ Boolean

Parameters:

  • filename (String)

Returns:

  • (Boolean)


92
93
94
# File 'lib/solargraph/workspace.rb', line 92

def has_file? filename
  source_hash.key?(filename)
end

#merge(source) ⇒ Boolean

Merge the source. A merge will update the existing source for the file or add it to the sources if the workspace is configured to include it. The source is ignored if the configuration excludes it.

Parameters:

Returns:

  • (Boolean)

    True if the source was added to the workspace



49
50
51
52
53
54
55
56
57
# File 'lib/solargraph/workspace.rb', line 49

def merge source
  unless directory == '*' || source_hash.key?(source.filename)
    # Reload the config to determine if a new source should be included
    @config = Solargraph::Workspace::Config.new(directory)
    return false unless config.calculated.include?(source.filename)
  end
  source_hash[source.filename] = source
  true
end

#remove(filename) ⇒ Boolean

Remove a source from the workspace. The source will not be removed if its file exists and the workspace is configured to include it.

Parameters:

  • filename (String)

Returns:

  • (Boolean)

    True if the source was removed from the workspace



74
75
76
77
78
# File 'lib/solargraph/workspace.rb', line 74

def remove filename
  return false unless source_hash.key?(filename)
  source_hash.delete filename
  true
end

#source(filename) ⇒ Solargraph::Source

Get a source by its filename.

Parameters:

  • filename (String)

Returns:



100
101
102
# File 'lib/solargraph/workspace.rb', line 100

def source filename
  source_hash[filename]
end

#sourcesArray<Solargraph::Source>



86
87
88
# File 'lib/solargraph/workspace.rb', line 86

def sources
  source_hash.values
end

#synchronize!(updater) ⇒ void

This method returns an undefined value.

Synchronize the workspace from the provided updater.

Parameters:



136
137
138
# File 'lib/solargraph/workspace.rb', line 136

def synchronize! updater
  source_hash[updater.filename] = source_hash[updater.filename].synchronize(updater)
end

#would_merge?(filename) ⇒ Boolean

Determine whether a file would be merged into the workspace.

Parameters:

  • filename (String)

Returns:

  • (Boolean)


63
64
65
66
67
# File 'lib/solargraph/workspace.rb', line 63

def would_merge? filename
  return true if directory == '*' || source_hash.include?(filename)
  @config = Solargraph::Workspace::Config.new(directory)
  config.calculated.include?(filename)
end

#would_require?(path) ⇒ Boolean

True if the path resolves to a file in the workspace's require paths.

Parameters:

  • path (String)

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'lib/solargraph/workspace.rb', line 108

def would_require? path
  require_paths.each do |rp|
    return true if File.exist?(File.join(rp, "#{path}.rb"))
  end
  false
end