{"action":"created","comment":{"url":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/comments/239609785","pull_request_review_id":182441962,"id":239609785,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIzOTYwOTc4NQ==","diff_hunk":"@@ -547,6 +548,81 @@ extension SwiftLanguageServer {\n     // FIXME: cancellation\n     _ = handle\n   }\n+\n+  func foldingRange(_ req: Request<FoldingRangeRequest>) {\n+\n+    guard let snapshot = documentManager.latestSnapshot(req.params.textDocument.url) else {\n+      log(\"failed to find snapshot for url \\(req.params.textDocument.url)\")\n+      req.reply(nil)\n+      return\n+    }\n+\n+    let skreq = SKRequestDictionary(sourcekitd: sourcekitd)\n+    skreq[keys.request] = requests.editor_open\n+    skreq[keys.name] = \"FoldingRanges:\" + snapshot.document.url.path\n+    skreq[keys.sourcetext] = snapshot.text\n+    skreq[keys.syntactic_only] = 1\n+\n+    let handle = sourcekitd.send(skreq) { [weak self] result in\n+      guard let self = self else { return }\n+      guard let dict = result.success else {\n+        req.reply(.failure(result.failure!))\n+        return\n+      }\n+\n+      guard let syntaxMap: SKResponseArray = dict[self.keys.syntaxmap],\n+            let substructure: SKResponseArray = dict[self.keys.substructure] else {\n+        return req.reply([])\n+      }\n+\n+      var ranges: [FoldingRange] = []\n+\n+      syntaxMap.forEach { _, value in\n+        if let kind: sourcekitd_uid_t = value[self.keys.kind],\n+           kind == self.values.syntaxtype_comment,\n+           let offset: Int = value[self.keys.offset],\n+           let start: Position = snapshot.positionOf(utf8Offset: offset),\n+           let length: Int = value[self.keys.length],\n+           // SourceKit marks the end of a comment as the first non-comment character\n+           // after it, so we subtract one to get the real comment range.\n+           let end: Position = snapshot.positionOf(utf8Offset: offset + length - 1) {","path":"Sources/SourceKit/sourcekitd/SwiftLanguageServer.swift","position":49,"original_position":49,"commit_id":"ed058d0d3779ac1cc5c8997d28a1e584dfbaf2cd","original_commit_id":"ed058d0d3779ac1cc5c8997d28a1e584dfbaf2cd","user":{"login":"benlangmuir","id":3412956,"node_id":"MDQ6VXNlcjM0MTI5NTY=","avatar_url":"https://avatars2.githubusercontent.com/u/3412956?v=4","gravatar_id":"","url":"https://api.github.com/users/benlangmuir","html_url":"https://github.com/benlangmuir","followers_url":"https://api.github.com/users/benlangmuir/followers","following_url":"https://api.github.com/users/benlangmuir/following{/other_user}","gists_url":"https://api.github.com/users/benlangmuir/gists{/gist_id}","starred_url":"https://api.github.com/users/benlangmuir/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benlangmuir/subscriptions","organizations_url":"https://api.github.com/users/benlangmuir/orgs","repos_url":"https://api.github.com/users/benlangmuir/repos","events_url":"https://api.github.com/users/benlangmuir/events{/privacy}","received_events_url":"https://api.github.com/users/benlangmuir/received_events","type":"User","site_admin":false},"body":"To clarify, I mean change `Position(line: N, utf16Index: 0)` to `Position(line: N-1, utf16Index: <length of line N-1>)`.  You can get the line length from the snapshot's LineTable.","created_at":"2018-12-06T20:52:16Z","updated_at":"2018-12-06T20:52:16Z","html_url":"https://github.com/apple/sourcekit-lsp/pull/24#discussion_r239609785","pull_request_url":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/24","author_association":"MEMBER","_links":{"self":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/comments/239609785"},"html":{"href":"https://github.com/apple/sourcekit-lsp/pull/24#discussion_r239609785"},"pull_request":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/24"}},"in_reply_to_id":239591057},"pull_request":{"url":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/24","id":234971095,"node_id":"MDExOlB1bGxSZXF1ZXN0MjM0OTcxMDk1","html_url":"https://github.com/apple/sourcekit-lsp/pull/24","diff_url":"https://github.com/apple/sourcekit-lsp/pull/24.diff","patch_url":"https://github.com/apple/sourcekit-lsp/pull/24.patch","issue_url":"https://api.github.com/repos/apple/sourcekit-lsp/issues/24","number":24,"state":"open","locked":false,"title":"Add \"textDocument/foldingRanges\"","user":{"login":"rockbruno","id":11647449,"node_id":"MDQ6VXNlcjExNjQ3NDQ5","avatar_url":"https://avatars1.githubusercontent.com/u/11647449?v=4","gravatar_id":"","url":"https://api.github.com/users/rockbruno","html_url":"https://github.com/rockbruno","followers_url":"https://api.github.com/users/rockbruno/followers","following_url":"https://api.github.com/users/rockbruno/following{/other_user}","gists_url":"https://api.github.com/users/rockbruno/gists{/gist_id}","starred_url":"https://api.github.com/users/rockbruno/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rockbruno/subscriptions","organizations_url":"https://api.github.com/users/rockbruno/orgs","repos_url":"https://api.github.com/users/rockbruno/repos","events_url":"https://api.github.com/users/rockbruno/events{/privacy}","received_events_url":"https://api.github.com/users/rockbruno/received_events","type":"User","site_admin":false},"body":"Hey there, I was reading about the LSP yesterday and wanted to take a shot at implementing the folding requests 🙂 \r\n\r\nAlthough I'm unsure how Xcode implemented it, some spelunking makes me believe you guys are using the`bodyoffsets` from `editor.open` requests, so that's how I tackled it.\r\n\r\nThe first commit works for perfectly idented bodies but stills fails for weird syntaxes like:\r\n\r\n```\r\nfunc foo() {\r\n\r\nreturn }\r\n```\r\n\r\nSince Xcode can treat these, I'll implement the full request to treat the edge cases.\r\n\r\n<img width=\"149\" alt=\"screen shot 2018-11-30 at 10 12 18 am\" src=\"https://user-images.githubusercontent.com/11647449/49289028-93abb200-f489-11e8-8809-4c0b0b8eabb7.png\">\r\n","created_at":"2018-11-30T12:24:45Z","updated_at":"2018-12-06T20:52:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"878d387dd65e2e3d69b9c128ccaa8e8d7000df59","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/24/commits","review_comments_url":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/24/comments","review_comment_url":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apple/sourcekit-lsp/issues/24/comments","statuses_url":"https://api.github.com/repos/apple/sourcekit-lsp/statuses/ed058d0d3779ac1cc5c8997d28a1e584dfbaf2cd","head":{"label":"rockbruno:foldingRange","ref":"foldingRange","sha":"ed058d0d3779ac1cc5c8997d28a1e584dfbaf2cd","user":{"login":"rockbruno","id":11647449,"node_id":"MDQ6VXNlcjExNjQ3NDQ5","avatar_url":"https://avatars1.githubusercontent.com/u/11647449?v=4","gravatar_id":"","url":"https://api.github.com/users/rockbruno","html_url":"https://github.com/rockbruno","followers_url":"https://api.github.com/users/rockbruno/followers","following_url":"https://api.github.com/users/rockbruno/following{/other_user}","gists_url":"https://api.github.com/users/rockbruno/gists{/gist_id}","starred_url":"https://api.github.com/users/rockbruno/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rockbruno/subscriptions","organizations_url":"https://api.github.com/users/rockbruno/orgs","repos_url":"https://api.github.com/users/rockbruno/repos","events_url":"https://api.github.com/users/rockbruno/events{/privacy}","received_events_url":"https://api.github.com/users/rockbruno/received_events","type":"User","site_admin":false},"repo":{"id":159708967,"node_id":"MDEwOlJlcG9zaXRvcnkxNTk3MDg5Njc=","name":"sourcekit-lsp","full_name":"rockbruno/sourcekit-lsp","private":false,"owner":{"login":"rockbruno","id":11647449,"node_id":"MDQ6VXNlcjExNjQ3NDQ5","avatar_url":"https://avatars1.githubusercontent.com/u/11647449?v=4","gravatar_id":"","url":"https://api.github.com/users/rockbruno","html_url":"https://github.com/rockbruno","followers_url":"https://api.github.com/users/rockbruno/followers","following_url":"https://api.github.com/users/rockbruno/following{/other_user}","gists_url":"https://api.github.com/users/rockbruno/gists{/gist_id}","starred_url":"https://api.github.com/users/rockbruno/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rockbruno/subscriptions","organizations_url":"https://api.github.com/users/rockbruno/orgs","repos_url":"https://api.github.com/users/rockbruno/repos","events_url":"https://api.github.com/users/rockbruno/events{/privacy}","received_events_url":"https://api.github.com/users/rockbruno/received_events","type":"User","site_admin":false},"html_url":"https://github.com/rockbruno/sourcekit-lsp","description":"Language Server Protocol implementation for Swift and C-based languages","fork":true,"url":"https://api.github.com/repos/rockbruno/sourcekit-lsp","forks_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/forks","keys_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/teams","hooks_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/hooks","issue_events_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/issues/events{/number}","events_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/events","assignees_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/assignees{/user}","branches_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/branches{/branch}","tags_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/tags","blobs_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/git/refs{/sha}","trees_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/statuses/{sha}","languages_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/languages","stargazers_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/stargazers","contributors_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/contributors","subscribers_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/subscribers","subscription_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/subscription","commits_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/commits{/sha}","git_commits_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/git/commits{/sha}","comments_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/comments{/number}","issue_comment_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/issues/comments{/number}","contents_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/contents/{+path}","compare_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/merges","archive_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/downloads","issues_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/issues{/number}","pulls_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/pulls{/number}","milestones_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/milestones{/number}","notifications_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/labels{/name}","releases_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/releases{/id}","deployments_url":"https://api.github.com/repos/rockbruno/sourcekit-lsp/deployments","created_at":"2018-11-29T18:19:46Z","updated_at":"2018-11-29T18:19:49Z","pushed_at":"2018-12-06T11:41:43Z","git_url":"git://github.com/rockbruno/sourcekit-lsp.git","ssh_url":"git@github.com:rockbruno/sourcekit-lsp.git","clone_url":"https://github.com/rockbruno/sourcekit-lsp.git","svn_url":"https://github.com/rockbruno/sourcekit-lsp","homepage":"","size":206,"stargazers_count":0,"watchers_count":0,"language":"Swift","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apple:master","ref":"master","sha":"6fc765199d7ea4657c09e3d7a5fba08096c53af9","user":{"login":"apple","id":10639145,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEwNjM5MTQ1","avatar_url":"https://avatars0.githubusercontent.com/u/10639145?v=4","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"repo":{"id":154773196,"node_id":"MDEwOlJlcG9zaXRvcnkxNTQ3NzMxOTY=","name":"sourcekit-lsp","full_name":"apple/sourcekit-lsp","private":false,"owner":{"login":"apple","id":10639145,"node_id":"MDEyOk9yZ2FuaXphdGlvbjEwNjM5MTQ1","avatar_url":"https://avatars0.githubusercontent.com/u/10639145?v=4","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"html_url":"https://github.com/apple/sourcekit-lsp","description":"Language Server Protocol implementation for Swift and C-based languages","fork":false,"url":"https://api.github.com/repos/apple/sourcekit-lsp","forks_url":"https://api.github.com/repos/apple/sourcekit-lsp/forks","keys_url":"https://api.github.com/repos/apple/sourcekit-lsp/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apple/sourcekit-lsp/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apple/sourcekit-lsp/teams","hooks_url":"https://api.github.com/repos/apple/sourcekit-lsp/hooks","issue_events_url":"https://api.github.com/repos/apple/sourcekit-lsp/issues/events{/number}","events_url":"https://api.github.com/repos/apple/sourcekit-lsp/events","assignees_url":"https://api.github.com/repos/apple/sourcekit-lsp/assignees{/user}","branches_url":"https://api.github.com/repos/apple/sourcekit-lsp/branches{/branch}","tags_url":"https://api.github.com/repos/apple/sourcekit-lsp/tags","blobs_url":"https://api.github.com/repos/apple/sourcekit-lsp/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apple/sourcekit-lsp/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apple/sourcekit-lsp/git/refs{/sha}","trees_url":"https://api.github.com/repos/apple/sourcekit-lsp/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apple/sourcekit-lsp/statuses/{sha}","languages_url":"https://api.github.com/repos/apple/sourcekit-lsp/languages","stargazers_url":"https://api.github.com/repos/apple/sourcekit-lsp/stargazers","contributors_url":"https://api.github.com/repos/apple/sourcekit-lsp/contributors","subscribers_url":"https://api.github.com/repos/apple/sourcekit-lsp/subscribers","subscription_url":"https://api.github.com/repos/apple/sourcekit-lsp/subscription","commits_url":"https://api.github.com/repos/apple/sourcekit-lsp/commits{/sha}","git_commits_url":"https://api.github.com/repos/apple/sourcekit-lsp/git/commits{/sha}","comments_url":"https://api.github.com/repos/apple/sourcekit-lsp/comments{/number}","issue_comment_url":"https://api.github.com/repos/apple/sourcekit-lsp/issues/comments{/number}","contents_url":"https://api.github.com/repos/apple/sourcekit-lsp/contents/{+path}","compare_url":"https://api.github.com/repos/apple/sourcekit-lsp/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apple/sourcekit-lsp/merges","archive_url":"https://api.github.com/repos/apple/sourcekit-lsp/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apple/sourcekit-lsp/downloads","issues_url":"https://api.github.com/repos/apple/sourcekit-lsp/issues{/number}","pulls_url":"https://api.github.com/repos/apple/sourcekit-lsp/pulls{/number}","milestones_url":"https://api.github.com/repos/apple/sourcekit-lsp/milestones{/number}","notifications_url":"https://api.github.com/repos/apple/sourcekit-lsp/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apple/sourcekit-lsp/labels{/name}","releases_url":"https://api.github.com/repos/apple/sourcekit-lsp/releases{/id}","deployments_url":"https://api.github.com/repos/apple/sourcekit-lsp/deployments","created_at":"2018-10-26T03:39:21Z","updated_at":"2018-12-06T20:21:31Z","pushed_at":"2018-12-06T18:10:10Z","git_url":"git://github.com/apple/sourcekit-lsp.git","ssh_url":"git@github.com:apple/sourcekit-lsp.git","clone_url":"https://github.com/apple/sourcekit-lsp.git","svn_url":"https://github.com/apple/sourcekit-lsp","homepage":"","size":232,"stargazers_count":868,"watchers_count":868,"language":"Swift","has_issues":false,"has_projects":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":32,"mirror_url":null,"archived":false,"open_issues_count":4,"license":{"key":"apache-2.0","name":"Apache License 2.0","spdx_id":"Apache-2.0","url":"https://api.github.com/licenses/apache-2.0","node_id":"MDc6TGljZW5zZTI="},"forks":32,"open_issues":4,"watchers":868,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/24"},"html":{"href":"https://github.com/apple/sourcekit-lsp/pull/24"},"issue":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/issues/24"},"comments":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/issues/24/comments"},"review_comments":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/24/comments"},"review_comment":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/pulls/24/commits"},"statuses":{"href":"https://api.github.com/repos/apple/sourcekit-lsp/statuses/ed058d0d3779ac1cc5c8997d28a1e584dfbaf2cd"}},"author_association":"NONE"}}
{"actor":{"display_login":"benlangmuir"}}