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: '')


13
14
15
16
17
# File 'lib/solargraph/workspace.rb', line 13

def initialize directory = '', config = nil
  @directory = directory
  @config = config
  load_sources
end

Instance Attribute Details

#directoryString (readonly)

Returns:

  • (String)


10
11
12
# File 'lib/solargraph/workspace.rb', line 10

def directory
  @directory
end

Instance Method Details

#configSolargraph::Workspace::Config



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

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

#filenamesArray<String>

Returns:

  • (Array<String>)


62
63
64
# File 'lib/solargraph/workspace.rb', line 62

def filenames
  source_hash.keys
end

#gemspec?Boolean

True if the workspace contains at least one gemspec file.

Returns:

  • (Boolean)


104
105
106
# File 'lib/solargraph/workspace.rb', line 104

def gemspec?
  !gemspecs.empty?
end

#gemspecsArray<String>

Get an array of all gemspec files in the workspace.

Returns:

  • (Array<String>)


111
112
113
114
# File 'lib/solargraph/workspace.rb', line 111

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

#has_file?(filename) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/solargraph/workspace.rb', line 72

def has_file? filename
  source_hash.has_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



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

def merge source
  unless source_hash.has_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



55
56
57
58
59
# File 'lib/solargraph/workspace.rb', line 55

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

#require_pathsArray<String>

The require paths associated with the workspace.

Returns:

  • (Array<String>)


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

def require_paths
  @require_paths ||= generate_require_paths
end

#source(filename) ⇒ Solargraph::Source

Get a source by its filename.

Returns:



79
80
81
# File 'lib/solargraph/workspace.rb', line 79

def source filename
  source_hash[filename]
end

#sourcesArray<Solargraph::Source>

Returns:



67
68
69
# File 'lib/solargraph/workspace.rb', line 67

def sources
  source_hash.values
end

#synchronize!(updater) ⇒ void

This method returns an undefined value.

Synchronize the workspace from the provided updater.

Parameters:



120
121
122
# File 'lib/solargraph/workspace.rb', line 120

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)


44
45
46
47
48
# File 'lib/solargraph/workspace.rb', line 44

def would_merge? filename
  return true if 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)


94
95
96
97
98
99
# File 'lib/solargraph/workspace.rb', line 94

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