Class: ThemeCheck::VersionedInMemoryStorage

Inherits:
InMemoryStorage show all
Defined in:
lib/theme_check/language_server/versioned_in_memory_storage.rb

Defined Under Namespace

Classes: Version

Instance Attribute Summary collapse

Attributes inherited from InMemoryStorage

#root

Instance Method Summary collapse

Methods inherited from InMemoryStorage

#directories, #files, #mkdir, #path, #read, #relative_path, #remove

Methods inherited from Storage

#directories, #files, #path, #read

Constructor Details

#initialize(files, root = "/dev/null") ⇒ VersionedInMemoryStorage

Returns a new instance of VersionedInMemoryStorage.



9
10
11
12
13
# File 'lib/theme_check/language_server/versioned_in_memory_storage.rb', line 9

def initialize(files, root = "/dev/null")
  super(files, root)
  @versions = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#versionsObject (readonly)

Returns the value of attribute versions.



7
8
9
# File 'lib/theme_check/language_server/versioned_in_memory_storage.rb', line 7

def versions
  @versions
end

Instance Method Details

#read_version(relative_path) ⇒ Object



57
58
59
# File 'lib/theme_check/language_server/versioned_in_memory_storage.rb', line 57

def read_version(relative_path)
  @mutex.synchronize { [read(relative_path), version(relative_path)] }
end

#version(relative_path) ⇒ Object



65
66
67
# File 'lib/theme_check/language_server/versioned_in_memory_storage.rb', line 65

def version(relative_path)
  @versions[relative_path.to_s]
end

#versioned?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/theme_check/language_server/versioned_in_memory_storage.rb', line 61

def versioned?
  true
end

#write(relative_path, content, version) ⇒ Object

Motivations:

- Need way for LanguageServer to know on which version of a file
  the check was run on, because we need to know where the
  TextEdit goes. If the text changed, our TextEdit might not be
  in the right spot. e.g.

  Example:

  ```
  Hi
  {{world}}
  ```

  Would produce two "SpaceInsideBrace" errors:

  - One after {{ at index 5 to 6
  - One before }} at index 10 to 11

  If the user goes in and changes Hi to Sup, and _then_
  right clicks to apply the code edit at index 5 to 6, he'd
  get the following:

  ```
  Sup
  { {world}}
  ```

  Which is not a fix at all.

Solution:

- Have the LanguageServer store the version on textDocument/did{Open,Change,Close}
- Have ThemeFile store the version right after @storage.read.
- Add version to the diagnostic meta data
- Use diagnostic meta data to determine if we can make a code edit or not
- Only offer fixes on "clean" files (or offer the change but specify the version so the editor knows what to do with it)


50
51
52
53
54
55
# File 'lib/theme_check/language_server/versioned_in_memory_storage.rb', line 50

def write(relative_path, content, version)
  @mutex.synchronize do
    @versions[relative_path] = version
    super(relative_path, content)
  end
end