Class: PlatformosCheck::VersionedInMemoryStorage

Inherits:
InMemoryStorage show all
Defined in:
lib/platformos_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

#files, #files_with_content, #mkdir, #path, #read, #relative_path

Methods inherited from Storage

#files, #path, #platformos_app, #read

Constructor Details

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

Returns a new instance of VersionedInMemoryStorage.



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

def initialize(files, root = "/dev/null")
  super(files, root)
  @versions = {} # Hash<relative_path, number>
  @mutex = Mutex.new
end

Instance Attribute Details

#versionsObject (readonly)

Returns the value of attribute versions.



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

def versions
  @versions
end

Instance Method Details

#opened_filesObject



80
81
82
# File 'lib/platformos_check/language_server/versioned_in_memory_storage.rb', line 80

def opened_files
  @versions.keys
end

#read_version(relative_path) ⇒ Object



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

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

#remove(relative_path) ⇒ Object



65
66
67
68
69
70
# File 'lib/platformos_check/language_server/versioned_in_memory_storage.rb', line 65

def remove(relative_path)
  @mutex.synchronize do
    @versions.delete(relative_path)
    super(relative_path)
  end
end

#version(relative_path) ⇒ Object



76
77
78
# File 'lib/platformos_check/language_server/versioned_in_memory_storage.rb', line 76

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

#versioned?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/platformos_check/language_server/versioned_in_memory_storage.rb', line 72

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 AppFile 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
56
57
58
59
# File 'lib/platformos_check/language_server/versioned_in_memory_storage.rb', line 50

def write(relative_path, content, version)
  @mutex.synchronize do
    if version.nil?
      @versions.delete(relative_path)
    else
      @versions[relative_path] = version
    end
    super(relative_path, content)
  end
end