SourceKit-LSPを使ってWebブラウザでSwiftの入力補完を実現する

 SourceKit-LSPを使ってWebブラウザでSwiftの入力補完を実現する

- SwiftFiddle (Online Playground) Demo
- 標準ライブラリの関数などをコード補完する
- SourceKit-LSPと通信する
- Text Document Synchronization
- システム構成と実装

9bf923e39671cde83584e3e926296c13?s=128

Kishikawa Katsumi

September 29, 2020
Tweet

Transcript

  1. Kishikawa Katsumi SourceKit-LSPΛ࢖ͬͯWeb ϒϥ΢βͰSwiftͷೖྗิ׬Λ࣮ݱ͢Δ

  2. ໨࣍ 1. SwiftFiddle (Online Playground) Demo 2. ඪ४ϥΠϒϥϦͷؔ਺ͳͲΛίʔυิ׬͢Δ 1. SourceKit-LSPͱ௨৴͢Δ

    2. Text Document Synchronization 3. γεςϜߏ੒ͱ࣮૷
  3. SwiftFiddle Demo https://www.swiftfiddle.com/

  4. None
  5. None
  6. None
  7. ඪ४ϥΠϒϥϦͷؔ਺ͳͲΛ ίʔυิ׬͢Δ

  8. None
  9. SourceKit-LSPͱ௨৴͢Δ Client SourceKit-LSP initialize initialized didOpen hover hover definitions hover

    references
  10. SourceKit-LSPͱ௨৴͢Δ Client SourceKit-LSP initialize didOpen hover { workspace: ~/projects/MySwiftApp }

    { documentURI: src/main.swift, text: "import Foundation ..." } { documentURI: src/main.swift, position: { row: 32, col: 7} }
  11. SourceKit-LSPͱ௨৴͢Δ Client SourceKit-LSP initialize didOpen hover { workspace: ~/projects/MySwiftApp }

    { documentURI: src/main.swift, text: "import Foundation ..." } { documentURI: src/main.swift, position: { row: 32, col: 7} }
  12. SourceKit-LSPͱ௨৴͢Δ Client SourceKit-LSP didOpen didChange { documentURI: src/main.swift, version: 2,

    contentChanges: ... } didChange { ..., version: 3, contentChanges: ... } completion
  13. Text Document Synchronization Client support for textDocument/didOpen, textDocument/ didChange and

    textDocument/didClose notifications is mandatory in the protocol and clients can not opt out supporting them. This includes both full and incremental syncronization in the textDocument/didChange notification. In addition a server must either implement all three of them or none. Their capabilities are therefore controlled via a combined client and server capability. https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_synchronization
  14. https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_synchronization

  15. Text Document Synchronization The actual content changes. The content changes

    describe single state changes to the document. So if there are two content changes c1 (at array index 0) and c2 (at array index 1) for a document in state S then c1 moves the document from S to S' and c2 from S' to S''. So c1 is computed on the state S and c2 is computed on the state S'. To mirror the content of a document using change events use the following approach: - start with the same initial content - apply the 'textDocument/didChange' notifications in the order you recevie them. - apply the `TextDocumentContentChangeEvent`s in a single notification in the order you receive them. https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_synchronization
  16. Text Document Synchronization S contentChange contentChange contentChange didChange

  17. Text Document Synchronization S contentChange contentChange contentChange didChange S′

  18. Text Document Synchronization S′ contentChange contentChange didChange S′′

  19. Text Document Synchronization /// A change to a text document.

    /// /// If `range` and `rangeLength` are unspecified, /// the whole document content is replaced. /// public struct TextDocumentContentChangeEvent: Codable, Hashable { https://github.com/apple/sourcekit-lsp
  20. Text Document Synchronization https://github.com/kishikawakatsumi/swift-playground

  21. None
  22. LSP

  23. None
  24. None
  25. None
  26. None
  27. None
  28. None
  29. None
  30. None
  31. GitHub Codespaces

  32. GitHub Codespaces

  33. References • https://swiftfiddle.com • https://github.com/kishikawakatsumi/swift-playground • https://github.com/kishikawakatsumi/SwiftFiddleEditor • https://github.com/kishikawakatsumi/SourceKitForSafari •

    https://github.com/apple/sourcekit-lsp • https://microsoft.github.io/language-server-protocol/ • https://ace.c9.io/