├── .editorconfig ├── .eslintignore ├── .eslintrc.js ├── .github ├── ISSUE_TEMPLATE │ └── issue-report.md ├── stale.yml └── workflows │ └── node.js.yml ├── .gitignore ├── .markdownlint.yaml ├── LICENSE ├── README.md ├── esbuild.mjs ├── package-lock.json ├── package.json ├── server └── com.microsoft.java.debug.plugin-0.51.0.jar ├── src ├── classFilter.ts ├── commands.ts ├── debugserver.ts ├── index.ts ├── protocol.ts ├── resources │ └── vimspector.json └── settings.ts ├── tsconfig.json └── update_server /.editorconfig: -------------------------------------------------------------------------------- 1 | ; EditorConfig is awesome: http://EditorConfig.org 2 | 3 | ; top-most EditorConfig file 4 | root = true 5 | 6 | ; base rules 7 | [*] 8 | indent_style = space 9 | indent_size = 2 10 | end_of_line = lf 11 | charset = utf-8 12 | trim_trailing_whitespace = true 13 | insert_final_newline = true 14 | 15 | ; Dont remove trailing whitespace from markdown 16 | ; files since it messes up newlines 17 | [*.md] 18 | trim_trailing_whitespace = false 19 | 20 | ; Make does not work without tabs 21 | [Makefile] 22 | indent_style = tab 23 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | lib/ 2 | !.eslintrc.js 3 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | env: { 4 | node: true, 5 | }, 6 | parser: '@typescript-eslint/parser', 7 | plugins: ['@typescript-eslint'], 8 | extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended'], 9 | rules: { 10 | '@typescript-eslint/ban-ts-comment': 'off', 11 | '@typescript-eslint/no-explicit-any': 'off', 12 | '@typescript-eslint/no-non-null-assertion': 'off', 13 | '@typescript-eslint/no-namespace': 'off', 14 | '@typescript-eslint/no-empty-function': 'off', 15 | '@typescript-eslint/explicit-function-return-type': 'off', 16 | '@typescript-eslint/explicit-module-boundary-types': 'off', 17 | }, 18 | }; 19 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/issue-report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Issue report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the issue** 11 | A clear and concise description of the problem. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Go to '...' 16 | 2. Open this file '....' 17 | 3. Run this command '....' 18 | 4. See error 19 | 20 | **Expected behavior** 21 | A clear and concise description of what you expected to happen. 22 | 23 | **Versions of relevant software** 24 | - vim: 25 | - coc.nvim: 26 | - coc-java: 27 | - vimspector: 28 | - java: 29 | - maven: 30 | 31 | **Relevant logs and error messages** 32 | * View the trace logging output for the Java language server: 33 | * Set `{ "java.trace.server": "verbose" }` in `:CocSettings`. 34 | * Run the command `:CocCommand workspace.showOutput java`. 35 | * Are there any errors? 36 | * View the Vimspector log `:VimspectorShowOutput Vimspector`. 37 | * Are the any errors? 38 | * After running the command `:CocCommand java.debug.vimspector.start` Do you see a message like `[coc.nvim] Java debug server started on port` in the output of `:messages`? 39 | 40 | **Additional context** 41 | Add any other context about the problem here. 42 | 43 | **Screenshots** 44 | If applicable, add screenshots to help explain your problem. 45 | -------------------------------------------------------------------------------- /.github/stale.yml: -------------------------------------------------------------------------------- 1 | --- 2 | # https://github.com/probot/stale 3 | # 4 | # Number of days of inactivity before an issue becomes stale 5 | daysUntilStale: 30 6 | # Number of days of inactivity before a stale issue is closed 7 | daysUntilClose: 7 8 | # Issues with these labels will never be considered stale 9 | exemptLabels: 10 | - keep 11 | - pinned 12 | # Label to use when marking an issue as stale 13 | staleLabel: wontfix 14 | # Comment to post when marking an issue as stale. Set to `false` to disable 15 | markComment: > 16 | This issue has been automatically marked as stale because it has not had 17 | recent activity. It will be closed if no further activity occurs. Thank you 18 | for your contributions. 19 | # Comment to post when closing a stale issue. Set to `false` to disable 20 | closeComment: > 21 | This issue has been automatically closed due to lack of activity. 22 | -------------------------------------------------------------------------------- /.github/workflows/node.js.yml: -------------------------------------------------------------------------------- 1 | # https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 2 | --- 3 | name: Node.js CI 4 | 5 | on: 6 | push: 7 | branches: [main] 8 | pull_request: 9 | branches: [main] 10 | 11 | jobs: 12 | build: 13 | 14 | runs-on: ubuntu-latest 15 | 16 | strategy: 17 | matrix: 18 | node-version: [16.x, 18.x, 20.x] 19 | 20 | steps: 21 | - uses: actions/checkout@v3 22 | - name: Use Node.js ${{ matrix.node-version }} 23 | uses: actions/setup-node@v3 24 | with: 25 | node-version: ${{ matrix.node-version }} 26 | cache: npm 27 | - name: Install dependencies 28 | run: npm ci 29 | - name: Run Linters 30 | run: npm run lint 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | /.vim 3 | /lib 4 | -------------------------------------------------------------------------------- /.markdownlint.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | MD013: 3 | line_length: 120 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 2.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 4 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION 5 | OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial content 12 | Distributed under this Agreement, and 13 | 14 | b) in the case of each subsequent Contributor: 15 | i) changes to the Program, and 16 | ii) additions to the Program; 17 | where such changes and/or additions to the Program originate from 18 | and are Distributed by that particular Contributor. A Contribution 19 | "originates" from a Contributor if it was added to the Program by 20 | such Contributor itself or anyone acting on such Contributor's behalf. 21 | Contributions do not include changes or additions to the Program that 22 | are not Modified Works. 23 | 24 | "Contributor" means any person or entity that Distributes the Program. 25 | 26 | "Licensed Patents" mean patent claims licensable by a Contributor which 27 | are necessarily infringed by the use or sale of its Contribution alone 28 | or when combined with the Program. 29 | 30 | "Program" means the Contributions Distributed in accordance with this 31 | Agreement. 32 | 33 | "Recipient" means anyone who receives the Program under this Agreement 34 | or any Secondary License (as applicable), including Contributors. 35 | 36 | "Derivative Works" shall mean any work, whether in Source Code or other 37 | form, that is based on (or derived from) the Program and for which the 38 | editorial revisions, annotations, elaborations, or other modifications 39 | represent, as a whole, an original work of authorship. 40 | 41 | "Modified Works" shall mean any work in Source Code or other form that 42 | results from an addition to, deletion from, or modification of the 43 | contents of the Program, including, for purposes of clarity any new file 44 | in Source Code form that contains any contents of the Program. Modified 45 | Works shall not include works that contain only declarations, 46 | interfaces, types, classes, structures, or files of the Program solely 47 | in each case in order to link to, bind by name, or subclass the Program 48 | or Modified Works thereof. 49 | 50 | "Distribute" means the acts of a) distributing or b) making available 51 | in any manner that enables the transfer of a copy. 52 | 53 | "Source Code" means the form of a Program preferred for making 54 | modifications, including but not limited to software source code, 55 | documentation source, and configuration files. 56 | 57 | "Secondary License" means either the GNU General Public License, 58 | Version 2.0, or any later versions of that license, including any 59 | exceptions or additional permissions as identified by the initial 60 | Contributor. 61 | 62 | 2. GRANT OF RIGHTS 63 | 64 | a) Subject to the terms of this Agreement, each Contributor hereby 65 | grants Recipient a non-exclusive, worldwide, royalty-free copyright 66 | license to reproduce, prepare Derivative Works of, publicly display, 67 | publicly perform, Distribute and sublicense the Contribution of such 68 | Contributor, if any, and such Derivative Works. 69 | 70 | b) Subject to the terms of this Agreement, each Contributor hereby 71 | grants Recipient a non-exclusive, worldwide, royalty-free patent 72 | license under Licensed Patents to make, use, sell, offer to sell, 73 | import and otherwise transfer the Contribution of such Contributor, 74 | if any, in Source Code or other form. This patent license shall 75 | apply to the combination of the Contribution and the Program if, at 76 | the time the Contribution is added by the Contributor, such addition 77 | of the Contribution causes such combination to be covered by the 78 | Licensed Patents. The patent license shall not apply to any other 79 | combinations which include the Contribution. No hardware per se is 80 | licensed hereunder. 81 | 82 | c) Recipient understands that although each Contributor grants the 83 | licenses to its Contributions set forth herein, no assurances are 84 | provided by any Contributor that the Program does not infringe the 85 | patent or other intellectual property rights of any other entity. 86 | Each Contributor disclaims any liability to Recipient for claims 87 | brought by any other entity based on infringement of intellectual 88 | property rights or otherwise. As a condition to exercising the 89 | rights and licenses granted hereunder, each Recipient hereby 90 | assumes sole responsibility to secure any other intellectual 91 | property rights needed, if any. For example, if a third party 92 | patent license is required to allow Recipient to Distribute the 93 | Program, it is Recipient's responsibility to acquire that license 94 | before distributing the Program. 95 | 96 | d) Each Contributor represents that to its knowledge it has 97 | sufficient copyright rights in its Contribution, if any, to grant 98 | the copyright license set forth in this Agreement. 99 | 100 | e) Notwithstanding the terms of any Secondary License, no 101 | Contributor makes additional grants to any Recipient (other than 102 | those set forth in this Agreement) as a result of such Recipient's 103 | receipt of the Program under the terms of a Secondary License 104 | (if permitted under the terms of Section 3). 105 | 106 | 3. REQUIREMENTS 107 | 108 | 3.1 If a Contributor Distributes the Program in any form, then: 109 | 110 | a) the Program must also be made available as Source Code, in 111 | accordance with section 3.2, and the Contributor must accompany 112 | the Program with a statement that the Source Code for the Program 113 | is available under this Agreement, and informs Recipients how to 114 | obtain it in a reasonable manner on or through a medium customarily 115 | used for software exchange; and 116 | 117 | b) the Contributor may Distribute the Program under a license 118 | different than this Agreement, provided that such license: 119 | i) effectively disclaims on behalf of all other Contributors all 120 | warranties and conditions, express and implied, including 121 | warranties or conditions of title and non-infringement, and 122 | implied warranties or conditions of merchantability and fitness 123 | for a particular purpose; 124 | 125 | ii) effectively excludes on behalf of all other Contributors all 126 | liability for damages, including direct, indirect, special, 127 | incidental and consequential damages, such as lost profits; 128 | 129 | iii) does not attempt to limit or alter the recipients' rights 130 | in the Source Code under section 3.2; and 131 | 132 | iv) requires any subsequent distribution of the Program by any 133 | party to be under a license that satisfies the requirements 134 | of this section 3. 135 | 136 | 3.2 When the Program is Distributed as Source Code: 137 | 138 | a) it must be made available under this Agreement, or if the 139 | Program (i) is combined with other material in a separate file or 140 | files made available under a Secondary License, and (ii) the initial 141 | Contributor attached to the Source Code the notice described in 142 | Exhibit A of this Agreement, then the Program may be made available 143 | under the terms of such Secondary Licenses, and 144 | 145 | b) a copy of this Agreement must be included with each copy of 146 | the Program. 147 | 148 | 3.3 Contributors may not remove or alter any copyright, patent, 149 | trademark, attribution notices, disclaimers of warranty, or limitations 150 | of liability ("notices") contained within the Program from any copy of 151 | the Program which they Distribute, provided that Contributors may add 152 | their own appropriate notices. 153 | 154 | 4. COMMERCIAL DISTRIBUTION 155 | 156 | Commercial distributors of software may accept certain responsibilities 157 | with respect to end users, business partners and the like. While this 158 | license is intended to facilitate the commercial use of the Program, 159 | the Contributor who includes the Program in a commercial product 160 | offering should do so in a manner which does not create potential 161 | liability for other Contributors. Therefore, if a Contributor includes 162 | the Program in a commercial product offering, such Contributor 163 | ("Commercial Contributor") hereby agrees to defend and indemnify every 164 | other Contributor ("Indemnified Contributor") against any losses, 165 | damages and costs (collectively "Losses") arising from claims, lawsuits 166 | and other legal actions brought by a third party against the Indemnified 167 | Contributor to the extent caused by the acts or omissions of such 168 | Commercial Contributor in connection with its distribution of the Program 169 | in a commercial product offering. The obligations in this section do not 170 | apply to any claims or Losses relating to any actual or alleged 171 | intellectual property infringement. In order to qualify, an Indemnified 172 | Contributor must: a) promptly notify the Commercial Contributor in 173 | writing of such claim, and b) allow the Commercial Contributor to control, 174 | and cooperate with the Commercial Contributor in, the defense and any 175 | related settlement negotiations. The Indemnified Contributor may 176 | participate in any such claim at its own expense. 177 | 178 | For example, a Contributor might include the Program in a commercial 179 | product offering, Product X. That Contributor is then a Commercial 180 | Contributor. If that Commercial Contributor then makes performance 181 | claims, or offers warranties related to Product X, those performance 182 | claims and warranties are such Commercial Contributor's responsibility 183 | alone. Under this section, the Commercial Contributor would have to 184 | defend claims against the other Contributors related to those performance 185 | claims and warranties, and if a court requires any other Contributor to 186 | pay any damages as a result, the Commercial Contributor must pay 187 | those damages. 188 | 189 | 5. NO WARRANTY 190 | 191 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT 192 | PERMITTED BY APPLICABLE LAW, THE PROGRAM IS PROVIDED ON AN "AS IS" 193 | BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR 194 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF 195 | TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR 196 | PURPOSE. Each Recipient is solely responsible for determining the 197 | appropriateness of using and distributing the Program and assumes all 198 | risks associated with its exercise of rights under this Agreement, 199 | including but not limited to the risks and costs of program errors, 200 | compliance with applicable laws, damage to or loss of data, programs 201 | or equipment, and unavailability or interruption of operations. 202 | 203 | 6. DISCLAIMER OF LIABILITY 204 | 205 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, AND TO THE EXTENT 206 | PERMITTED BY APPLICABLE LAW, NEITHER RECIPIENT NOR ANY CONTRIBUTORS 207 | SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 208 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION LOST 209 | PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 210 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 211 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 212 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE 213 | POSSIBILITY OF SUCH DAMAGES. 214 | 215 | 7. GENERAL 216 | 217 | If any provision of this Agreement is invalid or unenforceable under 218 | applicable law, it shall not affect the validity or enforceability of 219 | the remainder of the terms of this Agreement, and without further 220 | action by the parties hereto, such provision shall be reformed to the 221 | minimum extent necessary to make such provision valid and enforceable. 222 | 223 | If Recipient institutes patent litigation against any entity 224 | (including a cross-claim or counterclaim in a lawsuit) alleging that the 225 | Program itself (excluding combinations of the Program with other software 226 | or hardware) infringes such Recipient's patent(s), then such Recipient's 227 | rights granted under Section 2(b) shall terminate as of the date such 228 | litigation is filed. 229 | 230 | All Recipient's rights under this Agreement shall terminate if it 231 | fails to comply with any of the material terms or conditions of this 232 | Agreement and does not cure such failure in a reasonable period of 233 | time after becoming aware of such noncompliance. If all Recipient's 234 | rights under this Agreement terminate, Recipient agrees to cease use 235 | and distribution of the Program as soon as reasonably practicable. 236 | However, Recipient's obligations under this Agreement and any licenses 237 | granted by Recipient relating to the Program shall continue and survive. 238 | 239 | Everyone is permitted to copy and distribute copies of this Agreement, 240 | but in order to avoid inconsistency the Agreement is copyrighted and 241 | may only be modified in the following manner. The Agreement Steward 242 | reserves the right to publish new versions (including revisions) of 243 | this Agreement from time to time. No one other than the Agreement 244 | Steward has the right to modify this Agreement. The Eclipse Foundation 245 | is the initial Agreement Steward. The Eclipse Foundation may assign the 246 | responsibility to serve as the Agreement Steward to a suitable separate 247 | entity. Each new version of the Agreement will be given a distinguishing 248 | version number. The Program (including Contributions) may always be 249 | Distributed subject to the version of the Agreement under which it was 250 | received. In addition, after a new version of the Agreement is published, 251 | Contributor may elect to Distribute the Program (including its 252 | Contributions) under the new version. 253 | 254 | Except as expressly stated in Sections 2(a) and 2(b) above, Recipient 255 | receives no rights or licenses to the intellectual property of any 256 | Contributor under this Agreement, whether expressly, by implication, 257 | estoppel or otherwise. All rights in the Program not expressly granted 258 | under this Agreement are reserved. Nothing in this Agreement is intended 259 | to be enforceable by any entity that is not a Contributor or Recipient. 260 | No third-party beneficiary rights are created under this Agreement. 261 | 262 | Exhibit A - Form of Secondary Licenses Notice 263 | 264 | "This Source Code may also be made available under the following 265 | Secondary Licenses when the conditions for such availability set forth 266 | in the Eclipse Public License, v. 2.0 are satisfied: {name license(s), 267 | version(s), and exceptions or additional permissions here}." 268 | 269 | Simply including a copy of this Agreement, including this Exhibit A 270 | is not sufficient to license the Source Code under Secondary Licenses. 271 | 272 | If it is not possible or desirable to put the notice in a particular 273 | file, then You may include the notice in a location (such as a LICENSE 274 | file in a relevant directory) where a recipient would be likely to 275 | look for such a notice. 276 | 277 | You may add additional accurate notices of copyright ownership. 278 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # coc-java-debug 2 | 3 | An [extension][0] for [coc.nvim][17] to enable the [Java Debug Server][1] for the Java language server ([jdt.ls][2]) in 4 | Vim/Neovim. 5 | 6 | It also provides an easy way to launch [Vimspector][19] and connect it to the Java debug server. 7 | 8 | ## Features 9 | 10 | - Integration with [Vimspector][6]. 11 | - Launch Vimspector and connect to the Java debugger with a single command. 12 | - Provide Vimspector config substitutions for the following Java project values: 13 | - class paths 14 | - main class 15 | - module paths 16 | - project name 17 | - Configure Java debug settings. 18 | 19 | ## Requirements 20 | 21 | - Install the [coc.nvim][17] plugin. 22 | - Install the [Vimspector][5] plugin. 23 | - Install the [coc-java][4] extension. 24 | 25 | ## Quick Start 26 | 27 | Install the coc-java-debug extension. 28 | 29 | ```sh 30 | :CocInstall coc-java-debug 31 | ``` 32 | 33 | Open a Java file with a main method in Vim. 34 | 35 | Set a [breakpoint][15] in your main method. 36 | 37 | Execute this Vim command. 38 | 39 | ```sh 40 | :CocCommand java.debug.vimspector.start 41 | ``` 42 | 43 | ## Goals 44 | 45 | - Provide a simple integration between [Vimspector][19] and the [Java Debug Server][1]. 46 | - Provide Java-specific replacements for Vimspector's [native config][10]. 47 | - See `java.debug.vimspector.substitution.*` settings. 48 | - Support Vim and Neovim. 49 | 50 | ## Non-Goals 51 | 52 | - This project is not intended to be a clone of [vscode-java-debug][16]. 53 | 54 | ## Available commands 55 | 56 | The following `:CocCommand` options are provided: 57 | 58 | - `java.debug.vimspector.start`: Launch Vimspector and connect it to the [Java Debug Server][1]. 59 | - `java.debug.settings.update`: Sync local debug settings to [Java Debug Server][1]. Done automatically when Vimspector is started with `java.debug.vimspector.start` command. 60 | 61 | ### Command Arguments 62 | 63 | `java.debug.vimspector.start` 64 | 65 | - Optionally accepts a JSON string of settings to be passed to Vimspector using ["LaunchWithSettings"][8] internally. 66 | - This feature may be used to override values in the Vimspector config at runtime. 67 | - For example, it can be used to specify which configuration to load and a [replacement][10] value. 68 | 69 | ```viml 70 | :CocCommand java.debug.vimspector.start {"configuration":"Run Test","Test":"Name of the test"} 71 | ``` 72 | 73 | ## Supported settings 74 | 75 | The following settings are supported in [CocConfig][9]: 76 | 77 | ### java.debug.vimspector 78 | 79 | - `java.debug.vimspector.config.createIfNotExists` : If `true` then a `.vimspector.json` config will be created in the workspace if one doesn't already exist, defaults to `true`. 80 | - `java.debug.vimspector.substitution.adapterPort` : Specifies the [adapter port][11] substitution name for use in `.vimspector.json`. The actual port number will replace this value in the config when the debug server is started, defaults to `AdapterPort`. 81 | - `java.debug.vimspector.substitution.classPaths` : Specifies the 'class paths' substitution name for use in `.vimspector.json`. The actual class paths will replace this value in the Vimspector config, defaults to `ClassPaths`. 82 | - `java.debug.vimspector.substitution.mainClass` : Specifies the 'main class' substitution name for use in `.vimspector.json`. The actual main class will replace this value in the Vimspector config, defaults to `MainClass`. 83 | - `java.debug.vimspector.substitution.modulePaths` : Specifies the 'module paths' substitution name for use in `.vimspector.json`. The actual module paths will replace this value in the Vimspector config, defaults to `ModulePaths`. 84 | - `java.debug.vimspector.substitution.projectName` : Specifies the 'project name' substitution name for use in `.vimspector.json`. The actual project name will replace this value in the Vimspector config, defaults to `ProjectName`. 85 | 86 | ### java.debug.settings 87 | 88 | - `java.debug.logLevel`: minimum level of debugger logs that are sent to language server, defaults to `warn`. 89 | - `java.debug.settings.showHex`: show numbers in hex format in "Variables" viewlet, defaults to `false`. 90 | - `java.debug.settings.showStaticVariables`: show static variables in "Variables" viewlet, defaults to `false`. 91 | - `java.debug.settings.showQualifiedNames`: show fully qualified class names in "Variables" viewlet, defaults to `false`. 92 | - `java.debug.settings.showLogicalStructure`: show the logical structure for the Collection and Map classes in "Variables" viewlet, defaults to `true`. 93 | - `java.debug.settings.showToString`: show 'toString()' value for all classes that override 'toString' method in "Variables" viewlet, defaults to `true`. 94 | - `java.debug.settings.maxStringLength`: the maximum length of string displayed in "Variables" viewlet, the string longer than this length will be trimmed, defaults to `0` which means no trim is performed. 95 | - `java.debug.settings.numericPrecision`: the precision when formatting doubles in "Variables" viewlet. 96 | - `java.debug.settings.hotCodeReplace`: Reload the changed Java classes during debugging, defaults to `manual`. 97 | - `java.debug.settings.exceptionBreakpoint.exceptionTypes`: Specifies a set of exception types you want to break on, e.g. `java.lang.NullPointerException`. A specific exception type and its subclasses can be selected for caught exceptions, uncaught exceptions, or both can be selected. 98 | - `java.debug.settings.exceptionBreakpoint.allowClasses`: Specifies the allowed locations where the exception breakpoint can break on. Wildcard is supported, e.g. `java.*`, `*.Foo`. 99 | - `java.debug.settings.exceptionBreakpoint.skipClasses`: Skip the specified classes when breaking on exception. 100 | - `$JDK` - Skip the JDK classes from the default system bootstrap classpath, such as rt.jar, jrt-fs.jar. 101 | - `$Libraries` - Skip the classes from application libraries, such as Maven, Gradle dependencies. 102 | - `java.*` - Skip the specified classes. Wildcard is supported. 103 | - `java.lang.ClassLoader` - Skip the classloaders. 104 | - `java.debug.settings.stepping.skipClasses`: Skip the specified classes when stepping. 105 | - `$JDK` - Skip the JDK classes from the default system bootstrap classpath, such as rt.jar, jrt-fs.jar. 106 | - `$Libraries` - Skip the classes from application libraries, such as Maven, Gradle dependencies. 107 | - `java.*` - Skip the specified classes. Wildcard is supported. 108 | - `java.lang.ClassLoader` - Skip the classloaders. 109 | - `java.debug.settings.stepping.skipSynthetics`: Skip synthetic methods when stepping. 110 | - `java.debug.settings.stepping.skipStaticInitializers`: Skip static initializer methods when stepping. 111 | - `java.debug.settings.stepping.skipConstructors`: Skip constructor methods when stepping. 112 | - `java.debug.settings.jdwp.limitOfVariablesPerJdwpRequest`: The maximum number of variables or fields that can be requested in one JDWP request. The higher the value, the less frequently debuggee will be requested when expanding the variable view. Also a large number can cause JDWP request timeout. Defaults to 100. 113 | - `java.debug.settings.jdwp.requestTimeout`: The timeout (ms) of JDWP request when the debugger communicates with the target JVM. Defaults to 3000. 114 | - `java.debug.settings.jdwp.async`: Experimental: Controls whether the debugger is allowed to send JDWP commands asynchronously. Async mode can improve remote debugging response speed on high-latency networks. Defaults to `auto`, and automatically switch to async mode when the latency of a single jdwp request exceeds 15ms during attach debugging. 115 | - `auto` (Default) 116 | - `on` 117 | - `off` 118 | - `java.debug.settings.debugSupportOnDecompiledSource`: [Experimental]: Enable debugging support on the decompiled source code. Be aware that this feature may affect the loading speed of Call Stack Viewlet. You also need [Language Support for Java by Red Hat](https://marketplace.visualstudio.com/items?itemName=redhat.java)@1.20.0 or higher to use this feature. 119 | 120 | ## Usage and Setup 121 | 122 | ### Debug a Main Method 123 | 124 | This example will demonstrate how to load a Java program with a main method and debug it using Vimspector. 125 | 126 | If you don't have a `.vimspector.json` file in the root directory of your Java project then `coc-java-debug` will create 127 | one for you unless `java.debug.vimspector.config.createIfNotExists` is disabled. 128 | 129 | If you already have a `.vimspector.json` file then add the config below. 130 | 131 | ```json 132 | { 133 | "adapters": { 134 | "coc-java-debug": { 135 | "port": "${AdapterPort}" 136 | } 137 | }, 138 | "configurations": { 139 | "javaLaunch": { 140 | "default": true, 141 | "adapter": "coc-java-debug", 142 | "configuration": { 143 | "args": "${args}", 144 | "request": "launch", 145 | "projectName": "${ProjectName}", 146 | "mainClass": "${MainClass}", 147 | "classPaths": ["*${ClassPaths}"] 148 | }, 149 | "breakpoints": { 150 | "exception": { 151 | "caught": "N", 152 | "uncaught": "N" 153 | } 154 | } 155 | } 156 | } 157 | } 158 | ``` 159 | 160 | Next, open a Java file with a main method in Vim and set a [breakpoint][15] in your main method. 161 | 162 | Execute the command to start debugging. 163 | 164 | ```viml 165 | :CocCommand java.debug.vimspector.start 166 | ``` 167 | 168 | You will be prompted with 169 | 170 | > Enter value for args: 171 | 172 | If you don't have any program arguments just press your enter key. 173 | Otherwise, type in your args just as you would from a terminal and then press your enter key. 174 | 175 | At this point Vimspector should open and pause your Java program on the breakpoint you set. 176 | 177 | That's it! You may now [step debug][20] your way through your Java program from within Vim. 178 | 179 | ### Remote Debugging 180 | 181 | This example will demonstrate attaching to a Java program that is running with remote debugging enabled. This is useful 182 | for debugging tests or running services. 183 | 184 | #### Vimspector Attach Config 185 | 186 | Add the following contents to the `.vimspector.json` file in the root directory of your Java project. Note, don't change 187 | `"${AdapterPort}"`. See [issue #3][7] for an explanation of how this port value works. 188 | 189 | ```json 190 | { 191 | "adapters": { 192 | "coc-java-debug": { 193 | "port": "${AdapterPort}" 194 | } 195 | }, 196 | "configurations": { 197 | "javaAttach": { 198 | "default": true, 199 | "adapter": "coc-java-debug", 200 | "configuration": { 201 | "request": "attach", 202 | "host": "127.0.0.1", 203 | "port": "5005" 204 | }, 205 | "breakpoints": { 206 | "exception": { 207 | "caught": "N", 208 | "uncaught": "N" 209 | } 210 | } 211 | } 212 | } 213 | } 214 | ``` 215 | 216 | *Review the [Vimspector config][12] docs for what's possible within this file.* 217 | 218 | ##### Configure Vim 219 | 220 | This extension provides `:CocCommand java.debug.vimspector.start` to simplify launching Vimspector. 221 | 222 | *Note, it does not start your Java process in remote debug mode. An example of how to do that is covered below.* 223 | 224 | To further simplify, launching Vimspector, add the following config to your `~/.vimrc` file or wherever appropriate for 225 | your Vim setup. 226 | 227 | ```viml 228 | " Press F1 key to launch Vimspector 229 | nmap :CocCommand java.debug.vimspector.start 230 | ``` 231 | 232 | ##### Start the Remote Debug Session 233 | 234 | First, run a Java program with [remote debugging enabled][13]. 235 | Be sure it is configured to pause and wait for a remote connection on port `5005` for this example work. 236 | 237 | For a simple Java program. Create a `Hello.java` file with these contents. 238 | 239 | ```java 240 | public class Hello { 241 | public static void main(String[] args) { 242 | System.out.println("Hello World!"); 243 | } 244 | } 245 | ``` 246 | 247 | Next, run these commands from a shell to compile the program and then start it with remote debugging enabled. 248 | 249 | ```sh 250 | javac -g Hello.java 251 | java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=y Hello 252 | ``` 253 | 254 | If everything works correctly you will see this message. 255 | 256 | > Listening for transport dt_socket at address: 5005 257 | 258 | Now, open the file you want to debug in Vim and set a [breakpoint with Vimspector][15]. 259 | 260 | Finally, start the debug session in Vim by pressing your `F1` key or use your custom key mapping if you have altered the 261 | config from this example. This should result in Vimspector opening in a new tab in Vim with your Java program paused at 262 | the breakpoint you set. 263 | 264 | That's it! You may now [step debug][20] your way through a Java program from within Vim. 265 | 266 | *Note, if you use a Java debug port different than `5005` you will need to change that value in your `.vimspector.json` 267 | file. It is also possible to configure this port dynamically in Vimspector in the same manner as the debug adapter 268 | port.* 269 | 270 | *Also note, if you use Maven for builds you may start [remote debugging][14] for tests and then run Vimspector.* 271 | 272 | ```sh 273 | mvn test -Dmaven.surefire.debug 274 | ``` 275 | 276 | ##### Alternative Configuration (Optional) 277 | 278 | If you'd prefer to launch the Vimspector plugin directly with your own configuration then add something similar the 279 | following config to your `~/.vimrc` file or wherever appropriate for your Vim setup. 280 | 281 | *Note, this will bypass using the `:CocCommand` documented above to start the debug session.* 282 | 283 | ```viml 284 | function! JavaStartDebugCallback(err, port) 285 | execute "cexpr! 'Java debug started on port: " . a:port . "'" 286 | call vimspector#LaunchWithSettings({ "configuration": "Java Attach", "AdapterPort": a:port }) 287 | endfunction 288 | 289 | function JavaStartDebug() 290 | call CocActionAsync('runCommand', 'vscode.java.startDebugSession', function('JavaStartDebugCallback')) 291 | endfunction 292 | 293 | nmap :call JavaStartDebug() 294 | ``` 295 | 296 | This example provides a way to start the Java debug server through coc.vim and then tell Vimspector which port to use to 297 | connect to the debug server. It maps the `F1` key to kick things off, but you can change this key mapping to whatever 298 | you want. 299 | 300 | ## License 301 | 302 | EPL 2.0, See [LICENSE](LICENSE) for more information. 303 | 304 | [0]: https://github.com/neoclide/coc.nvim/wiki/Using-coc-extensions 305 | [1]: https://github.com/Microsoft/java-debug 306 | [2]: https://github.com/eclipse/eclipse.jdt.ls 307 | [4]: https://github.com/neoclide/coc-java#quick-start 308 | [5]: https://github.com/puremourning/vimspector#installation 309 | [6]: https://puremourning.github.io/vimspector-web/ 310 | [7]: https://github.com/dansomething/coc-java-debug/issues/3#issuecomment-622075010 311 | [8]: https://github.com/puremourning/vimspector#launch-with-options 312 | [9]: https://github.com/neoclide/coc.nvim/wiki/Using-the-configuration-file#configuration-file-resolve 313 | [10]: https://puremourning.github.io/vimspector/configuration.html#replacements-and-variables 314 | [11]: https://puremourning.github.io/vimspector/configuration.html#adapter-configurations 315 | [12]: https://puremourning.github.io/vimspector/configuration.html 316 | [13]: https://docs.oracle.com/javase/8/docs/technotes/guides/jpda/conninv.html#Invocation 317 | [14]: https://maven.apache.org/surefire/maven-surefire-plugin/examples/debugging.html 318 | [15]: https://github.com/puremourning/vimspector?tab=readme-ov-file#breakpoints 319 | [16]: https://github.com/microsoft/vscode-java-debug 320 | [17]: https://github.com/neoclide/coc.nvim 321 | [19]: https://github.com/puremourning/vimspector?tab=readme-ov-file#other-lsp-clients 322 | [20]: https://github.com/puremourning/vimspector?tab=readme-ov-file#stepping 323 | -------------------------------------------------------------------------------- /esbuild.mjs: -------------------------------------------------------------------------------- 1 | import * as esbuild from 'esbuild'; 2 | 3 | const options = { 4 | entryPoints: ['src/index.ts'], 5 | bundle: true, 6 | minify: process.env.NODE_ENV !== 'development', 7 | sourcemap: process.env.NODE_ENV === 'development', 8 | mainFields: ['module', 'main'], 9 | external: ['coc.nvim'], 10 | platform: 'node', 11 | target: 'node16', 12 | outfile: 'lib/index.js', 13 | // https://esbuild.github.io/api/#log-level 14 | logLevel: process.env.NODE_ENV === 'development' ? 'info' : 'error', 15 | }; 16 | 17 | if (process.argv.length > 2 && process.argv[2] === '--watch') { 18 | const ctx = await esbuild.context(options); 19 | await ctx.watch(); 20 | console.log('watching...'); 21 | } else { 22 | const result = await esbuild.build(options); 23 | if (result.errors.length) { 24 | console.error(result.errors); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "coc-java-debug", 3 | "version": "1.0.0", 4 | "description": "Java langauage debugging extension for coc.nvim", 5 | "author": "dansomething", 6 | "license": "EPL-2.0", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/dansomething/coc-java-debug" 10 | }, 11 | "bugs": "https://github.com/dansomething/coc-java-debug/issues", 12 | "main": "lib/index.js", 13 | "files": [ 14 | "/LICENSE", 15 | "/README.md", 16 | "/lib/index.js", 17 | "/server" 18 | ], 19 | "keywords": [ 20 | "coc.nvim", 21 | "java", 22 | "debug", 23 | "debugging", 24 | "debugger", 25 | "vimspector" 26 | ], 27 | "engines": { 28 | "coc": ">=0.0.80", 29 | "node": ">=10.0.0" 30 | }, 31 | "scripts": { 32 | "build": "node esbuild.mjs", 33 | "clean": "rimraf lib", 34 | "fix": "eslint --fix .", 35 | "lint": "eslint .", 36 | "prepare": "npm-run-all clean build", 37 | "release": "np --no-tests", 38 | "watch": "node esbuild.mjs --watch" 39 | }, 40 | "prettier": { 41 | "singleQuote": true, 42 | "printWidth": 120, 43 | "semi": true 44 | }, 45 | "devDependencies": { 46 | "@types/node": "^12.20.55", 47 | "@typescript-eslint/eslint-plugin": "7.0.1", 48 | "@typescript-eslint/parser": "7.0.1", 49 | "coc.nvim": "^0.0.82", 50 | "esbuild": "^0.23.0", 51 | "eslint": "^8.57.0", 52 | "eslint-config-prettier": "^9.1.0", 53 | "eslint-plugin-prettier": "^5.2.1", 54 | "np": "^10.0.7", 55 | "npm-run-all": "^4.1.5", 56 | "prettier": "^3.3.3", 57 | "rimraf": "^5.0.5", 58 | "typescript": "^4.9.5" 59 | }, 60 | "activationEvents": [ 61 | "onLanguage:java" 62 | ], 63 | "contributes": { 64 | "breakpoints": [ 65 | { 66 | "language": "java" 67 | } 68 | ], 69 | "javaExtensions": [ 70 | "./server/com.microsoft.java.debug.plugin-0.51.0.jar" 71 | ], 72 | "configuration": { 73 | "type": "object", 74 | "title": "Java Debug Configuration", 75 | "properties": { 76 | "java.debug.vimspector.config.createIfNotExists": { 77 | "type": "boolean", 78 | "default": "true", 79 | "description": "Determines if a .vimspector.json config should be created in the workspace if one doesn't already exist.", 80 | "scope": "window" 81 | }, 82 | "java.debug.vimspector.substitution.adapterPort": { 83 | "type": "string", 84 | "default": "AdapterPort", 85 | "description": "Specifies the adapter port substitution name in `.vimspector.json`. The actual port number will replace this value in the Vimspector config when the Java debug server is started.", 86 | "scope": "window" 87 | }, 88 | "java.debug.vimspector.substitution.classPaths": { 89 | "type": "string", 90 | "default": "ClassPaths", 91 | "description": "Specifies the class paths substitution name in `.vimspector.json`. The actual Java class paths will replace this value in the Vimspector config.", 92 | "scope": "window" 93 | }, 94 | "java.debug.vimspector.substitution.mainClass": { 95 | "type": "string", 96 | "default": "MainClass", 97 | "description": "Specifies the main class substitution name in `.vimspector.json`. The actual Java main class will replace this value in the Vimspector config.", 98 | "scope": "window" 99 | }, 100 | "java.debug.vimspector.substitution.modulePaths": { 101 | "type": "string", 102 | "default": "ModulePaths", 103 | "description": "Specifies the module paths substitution name in `.vimspector.json`. The actual Java module paths will replace this value in the Vimspector config.", 104 | "scope": "window" 105 | }, 106 | "java.debug.vimspector.substitution.projectName": { 107 | "type": "string", 108 | "default": "ProjectName", 109 | "description": "Specifies the project name substitution name in `.vimspector.json`. The actual Java project name will replace this value in the Vimspector config.", 110 | "scope": "window" 111 | }, 112 | "java.debug.logLevel": { 113 | "type": "string", 114 | "default": "warn", 115 | "description": "minimum level of debugger logs that are sent to language server", 116 | "enum": [ 117 | "error", 118 | "warn", 119 | "info", 120 | "verbose" 121 | ], 122 | "scope": "window" 123 | }, 124 | "java.debug.settings.showHex": { 125 | "type": "boolean", 126 | "default": false, 127 | "description": "show numbers in hex format in `Variables` viewlet", 128 | "scope": "window" 129 | }, 130 | "java.debug.settings.showStaticVariables": { 131 | "type": "boolean", 132 | "default": false, 133 | "description": "Show static variables in `Variables` viewlet", 134 | "scope": "window" 135 | }, 136 | "java.debug.settings.showQualifiedNames": { 137 | "type": "boolean", 138 | "default": false, 139 | "description": "show fully qualified class names in `Variables` viewlet", 140 | "scope": "window" 141 | }, 142 | "java.debug.settings.showLogicalStructure": { 143 | "type": "boolean", 144 | "default": true, 145 | "description": "show the logical structure for the Collection and Map classes in `Variables` viewlet", 146 | "scope": "window" 147 | }, 148 | "java.debug.settings.showToString": { 149 | "type": "boolean", 150 | "default": true, 151 | "description": "show 'toString()' value for all classes that override 'toString' method in `Variables` viewlet", 152 | "scope": "window" 153 | }, 154 | "java.debug.settings.maxStringLength": { 155 | "type": "number", 156 | "default": 0, 157 | "description": "the maximum length of string displayed in `Variables` viewlet, the string longer than this length will be trimmed, defaults to 0 which means no trim is performed", 158 | "scope": "window" 159 | }, 160 | "java.debug.settings.numericPrecision": { 161 | "type": "number", 162 | "default": 0, 163 | "description": "the precision when formatting doubles in `Variables` viewlet", 164 | "scope": "window" 165 | }, 166 | "java.debug.settings.hotCodeReplace": { 167 | "type": "string", 168 | "default": "manual", 169 | "description": "Reload the changed Java classes during debugging", 170 | "enum": [ 171 | "auto", 172 | "manual", 173 | "never" 174 | ], 175 | "scope": "window" 176 | }, 177 | "java.debug.settings.exceptionBreakpoint.exceptionTypes": { 178 | "type": "array", 179 | "default": [], 180 | "description": "Specifies a set of exception types you want to break on", 181 | "scope": "window" 182 | }, 183 | "java.debug.settings.exceptionBreakpoint.allowClasses": { 184 | "type": "array", 185 | "default": [], 186 | "description": "Specifies the allowed locations where the exception breakpoint can break on. Wildcard is supported, e.g. java.*, *.Foo", 187 | "scope": "window" 188 | }, 189 | "java.debug.settings.exceptionBreakpoint.skipClasses": { 190 | "type": "array", 191 | "default": [], 192 | "description": "Skip the specified classes when breaking on exception", 193 | "items": { 194 | "anyOf": [ 195 | { 196 | "enum": [ 197 | "$JDK", 198 | "$Libraries", 199 | "java.lang.ClassLoader", 200 | "" 201 | ], 202 | "enumDescriptions": [ 203 | "Skip the JDK classes from the default system bootstrap classpath, such as rt.jar, jrt-fs.jar", 204 | "Skip the classes from application libraries, such as Maven, Gradle dependencies", 205 | "Skip the classloaders", 206 | "Skip the specified classes. Wildcard is supported" 207 | ] 208 | }, 209 | "string" 210 | ] 211 | }, 212 | "scope": "window" 213 | }, 214 | "java.debug.settings.stepping.skipClasses": { 215 | "type": "array", 216 | "default": [], 217 | "description": "Skip the specified classes when stepping", 218 | "items": { 219 | "anyOf": [ 220 | { 221 | "enum": [ 222 | "$JDK", 223 | "$Libraries", 224 | "java.lang.ClassLoader", 225 | "" 226 | ], 227 | "enumDescriptions": [ 228 | "Skip the JDK classes from the default system bootstrap classpath, such as rt.jar, jrt-fs.jar", 229 | "Skip the classes from application libraries, such as Maven, Gradle dependencies", 230 | "Skip the classloaders", 231 | "Skip the specified classes. Wildcard is supported" 232 | ] 233 | }, 234 | "string" 235 | ] 236 | }, 237 | "scope": "window" 238 | }, 239 | "java.debug.settings.stepping.skipSynthetics": { 240 | "type": "boolean", 241 | "default": false, 242 | "description": "Skip synthetic methods when stepping", 243 | "scope": "window" 244 | }, 245 | "java.debug.settings.stepping.skipStaticInitializers": { 246 | "type": "boolean", 247 | "default": false, 248 | "description": "Skip static initializer methods when stepping", 249 | "scope": "window" 250 | }, 251 | "java.debug.settings.stepping.skipConstructors": { 252 | "type": "boolean", 253 | "default": false, 254 | "description": "Skip constructor methods when stepping", 255 | "scope": "window" 256 | }, 257 | "java.debug.settings.jdwp.limitOfVariablesPerJdwpRequest": { 258 | "type": "number", 259 | "default": 100, 260 | "minimum": 1, 261 | "description": "The maximum number of variables or fields that can be requested in one JDWP request", 262 | "scope": "window" 263 | }, 264 | "java.debug.settings.jdwp.requestTimeout": { 265 | "type": "number", 266 | "default": 3000, 267 | "minimum": 100, 268 | "description": "The timeout (ms) of JDWP request when the debugger communicates with the target JVM", 269 | "scope": "window" 270 | }, 271 | "java.debug.settings.jdwp.async": { 272 | "type": "string", 273 | "default": "off", 274 | "description": "Experimental: Controls whether the debugger is allowed to send JDWP commands asynchronously", 275 | "enum": [ 276 | "auto", 277 | "on", 278 | "off" 279 | ], 280 | "scope": "window" 281 | }, 282 | "java.debug.settings.debugSupportOnDecompiledSource": { 283 | "type": "string", 284 | "default": "off", 285 | "description": "Experimental: Enable debugging support on the decompiled source code", 286 | "enum": [ 287 | "on", 288 | "off" 289 | ], 290 | "scope": "window" 291 | } 292 | } 293 | }, 294 | "commands": [ 295 | { 296 | "command": "java.debug.vimspector.start", 297 | "title": "Launch Vimspector and connect it to the Java Debug Server.", 298 | "category": "Java" 299 | }, 300 | { 301 | "command": "java.debug.settings.update", 302 | "title": "Update debug settings.", 303 | "category": "Java" 304 | }, 305 | { 306 | "command": "java.debug.resolveMainMethod", 307 | "title": "Show resolved main methods.", 308 | "category": "Java" 309 | }, 310 | { 311 | "command": "java.debug.resolveClasspath", 312 | "title": "Show resolved class paths.", 313 | "category": "Java" 314 | } 315 | ] 316 | } 317 | } 318 | -------------------------------------------------------------------------------- /server/com.microsoft.java.debug.plugin-0.51.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dansomething/coc-java-debug/cf3671962c8a3697f117e3e2f68b7faf91ecdcef/server/com.microsoft.java.debug.plugin-0.51.0.jar -------------------------------------------------------------------------------- /src/classFilter.ts: -------------------------------------------------------------------------------- 1 | import { Commands, executeCommand } from './commands'; 2 | 3 | export async function substituteFilterVariables(skipClasses: string[]): Promise { 4 | if (!skipClasses) { 5 | return []; 6 | } 7 | 8 | try { 9 | // Preprocess skipClasses configurations. 10 | if (Array.isArray(skipClasses)) { 11 | const hasReservedName = skipClasses.some((filter) => filter === '$JDK' || filter === '$Libraries'); 12 | return hasReservedName ? await executeCommand(Commands.JAVA_RESOLVE_CLASSFILTERS, skipClasses) : skipClasses; 13 | } else { 14 | console.error('Invalid type for skipClasses config:' + skipClasses); 15 | } 16 | } catch (e) { 17 | console.error(e); 18 | } 19 | 20 | return []; 21 | } 22 | -------------------------------------------------------------------------------- /src/commands.ts: -------------------------------------------------------------------------------- 1 | import { commands } from 'coc.nvim'; 2 | 3 | export namespace Commands { 4 | export const JAVA_START_DEBUGSESSION = 'vscode.java.startDebugSession'; 5 | 6 | export const JAVA_RESOLVE_CLASSPATH = 'vscode.java.resolveClasspath'; 7 | 8 | export const JAVA_RESOLVE_MAINMETHOD = 'vscode.java.resolveMainMethod'; 9 | 10 | export const JAVA_RESOLVE_CLASSFILTERS = 'vscode.java.resolveClassFilters'; 11 | 12 | export const JAVA_UPDATE_DEBUG_SETTINGS = 'vscode.java.updateDebugSettings'; 13 | 14 | export const JAVA_DEBUG_VIMSPECTOR_START = 'java.debug.vimspector.start'; 15 | 16 | export const JAVA_DEBUG_SETTINGS_UPDATE = 'java.debug.settings.update'; 17 | 18 | export const JAVA_DEBUG_RESOLVE_MAINMETHOD = 'java.debug.resolveMainMethod'; 19 | 20 | export const JAVA_DEBUG_RESOLVE_CLASSPATH = 'java.debug.resolveClasspath'; 21 | 22 | export const EXECUTE_WORKSPACE_COMMAND = 'java.execute.workspaceCommand'; 23 | } 24 | 25 | export async function executeCommand(...rest: any[]): Promise { 26 | return commands.executeCommand(Commands.EXECUTE_WORKSPACE_COMMAND, ...rest); 27 | } 28 | -------------------------------------------------------------------------------- /src/debugserver.ts: -------------------------------------------------------------------------------- 1 | import { TextDocument, Uri, window, workspace } from 'coc.nvim'; 2 | import { Commands, executeCommand } from './commands'; 3 | import { IClassPath, IMainClassOption, MainMethodResult } from './protocol'; 4 | 5 | export async function resolveMainMethodCurrentFile(): Promise { 6 | const mainMethods = await resolveMainMethodsCurrentFile(); 7 | if (mainMethods.length === 1) { 8 | return mainMethods[0]; 9 | } else if (mainMethods.length > 1) { 10 | return await pickMainMethod(mainMethods); 11 | } 12 | return undefined; 13 | } 14 | 15 | export async function resolveMainMethodsCurrentFile(): Promise { 16 | const { document } = await workspace.getCurrentState(); 17 | return resolveMainMethod(document); 18 | } 19 | 20 | async function resolveMainMethod(document: TextDocument): Promise { 21 | const resourcePath = getJavaResourcePath(document); 22 | return executeCommand(Commands.JAVA_RESOLVE_MAINMETHOD, resourcePath); 23 | } 24 | 25 | export async function resolveClassPathCurrentFile(): Promise { 26 | const mainMethod = await resolveMainMethodCurrentFile(); 27 | if (mainMethod) { 28 | return resolveClassPathMainMethod(mainMethod); 29 | } 30 | return { modulePaths: [], classPaths: [] }; 31 | } 32 | 33 | export async function resolveClassPathMainMethod(mainMethod: IMainClassOption): Promise { 34 | const classPath: any[] = await resolveClasspath(mainMethod.mainClass, mainMethod.projectName || ''); 35 | const [modulePaths, classPaths] = classPath; 36 | return { modulePaths, classPaths }; 37 | } 38 | 39 | async function resolveClasspath(mainClass: string, projectName: string, scope?: string): Promise { 40 | return executeCommand(Commands.JAVA_RESOLVE_CLASSPATH, mainClass, projectName, scope); 41 | } 42 | 43 | function getJavaResourcePath(document: TextDocument): string | undefined { 44 | const resource = Uri.parse(document.uri); 45 | if (resource.scheme === 'file' && resource.fsPath.endsWith('.java')) { 46 | return resource.toString(); 47 | } 48 | return undefined; 49 | } 50 | 51 | export async function pickMainMethod(mainMethods: MainMethodResult): Promise { 52 | const items = mainMethods.map((method) => { 53 | return method.mainClass; 54 | }); 55 | const selected = await window.showQuickpick(items, 'Choose a main method.'); 56 | // Choose the first one if none is selected. 57 | return mainMethods[selected >= 0 ? selected : 0]; 58 | } 59 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { ExtensionContext, commands, window, workspace } from 'coc.nvim'; 2 | import { Commands, executeCommand } from './commands'; 3 | import { 4 | resolveClassPathCurrentFile, 5 | resolveClassPathMainMethod, 6 | resolveMainMethodCurrentFile, 7 | resolveMainMethodsCurrentFile, 8 | } from './debugserver'; 9 | import { ISubstitutionVar } from './protocol'; 10 | import { onConfigurationChange, updateDebugSettings } from './settings'; 11 | import fs from 'fs'; 12 | import vimspectorJson from './resources/vimspector.json'; 13 | import * as path from 'path'; 14 | 15 | const VIMSPECTOR_CONFIG_FILE = '.vimspector.json'; 16 | 17 | export async function activate(context: ExtensionContext): Promise { 18 | registerCommands(context); 19 | context.subscriptions.push(onConfigurationChange()); 20 | } 21 | 22 | function registerCommands(context: ExtensionContext): void { 23 | context.subscriptions.push(commands.registerCommand(Commands.JAVA_DEBUG_VIMSPECTOR_START, startVimspector)); 24 | context.subscriptions.push(commands.registerCommand(Commands.JAVA_DEBUG_SETTINGS_UPDATE, updateDebugSettings)); 25 | context.subscriptions.push( 26 | commands.registerCommand(Commands.JAVA_DEBUG_RESOLVE_MAINMETHOD, showCommandResult(resolveMainMethodsCurrentFile)), 27 | ); 28 | context.subscriptions.push( 29 | commands.registerCommand(Commands.JAVA_DEBUG_RESOLVE_CLASSPATH, showCommandResult(resolveClassPathCurrentFile)), 30 | ); 31 | } 32 | 33 | async function startVimspector(...args: any[]): Promise { 34 | const debugPort: string = await executeCommand(Commands.JAVA_START_DEBUGSESSION); 35 | const msg = `Java debug server started on port: ${debugPort}`; 36 | console.info(msg); 37 | window.showInformationMessage(msg); 38 | 39 | updateDebugSettings(); 40 | initVimspectorConfig(); 41 | 42 | const mainMethod = await resolveMainMethodCurrentFile(); 43 | const mainClass = mainMethod?.mainClass; 44 | const projectName = mainMethod?.projectName; 45 | let modulePaths: string | undefined = undefined; 46 | let classPaths: string | undefined = undefined; 47 | if (mainMethod) { 48 | const classPathMainMethod = await resolveClassPathMainMethod(mainMethod); 49 | // Join path parts together to support using them with the Vimspector splat operator. 50 | // See https://puremourning.github.io/vimspector/configuration.html#the-splat-operator 51 | modulePaths = classPathMainMethod?.modulePaths.join(' '); 52 | classPaths = classPathMainMethod?.classPaths.join(' '); 53 | } 54 | 55 | const debugConfig = workspace.getConfiguration('java.debug'); 56 | // See package.json#configuration.properties 57 | const vars = debugConfig.get('vimspector.substitution'); 58 | const overrides = getOverrides(args); 59 | 60 | const settings = { 61 | [vars?.adapterPort as string]: debugPort, 62 | [vars?.classPaths as string]: classPaths, 63 | [vars?.mainClass as string]: mainClass, 64 | [vars?.modulePaths as string]: modulePaths, 65 | [vars?.projectName as string]: projectName, 66 | ...overrides, 67 | }; 68 | 69 | const vimspectorSettings = JSON.stringify(settings); 70 | // See https://github.com/puremourning/vimspector#launch-with-options 71 | // View logs with :CocOpenLog 72 | console.info(`Launching Vimspector with settings: ${vimspectorSettings}`); 73 | return workspace.nvim.eval(`vimspector#LaunchWithSettings(${vimspectorSettings})`); 74 | } 75 | 76 | /** 77 | * Converts the input command arguments to an object to be applied as 78 | * Vimspector settings overides. 79 | * 80 | * This also handles the possibily of the command args being split by spaces 81 | * before being passed to the callback. 82 | */ 83 | function getOverrides(rawArguments: any[]): any { 84 | let args = ''; 85 | if (rawArguments.length == 0) { 86 | args = rawArguments[0]; 87 | } else if (rawArguments.length >= 1) { 88 | const a: any[] = []; 89 | for (const v of rawArguments) { 90 | a.push(v); 91 | } 92 | args = a.join(' '); 93 | } 94 | return parseOverrides(args); 95 | } 96 | 97 | function parseOverrides(args: string): any { 98 | let overrides = {}; 99 | if (args) { 100 | try { 101 | overrides = JSON.parse(args); 102 | } catch (e) { 103 | window.showErrorMessage(`Expected valid JSON for Vimspector settings, but got: ${args}`, 'error'); 104 | } 105 | } 106 | return overrides; 107 | } 108 | 109 | function showCommandResult(func: () => Promise): (...args: any[]) => Promise { 110 | return async () => { 111 | const result = await func(); 112 | const json = JSON.stringify(result, null, 2); 113 | return window.showInformationMessage(json); 114 | }; 115 | } 116 | 117 | function initVimspectorConfig(): void { 118 | const config = workspace.getConfiguration('java.debug.vimspector.config'); 119 | const shouldCreate = config.get('createIfNotExists'); 120 | if (!shouldCreate) { 121 | console.debug(`Vimspector default config creation is not enabled. Skipping creation.`); 122 | return; 123 | } 124 | 125 | const configPath = path.resolve(workspace.root, VIMSPECTOR_CONFIG_FILE); 126 | if (fs.existsSync(configPath)) { 127 | console.debug(`${VIMSPECTOR_CONFIG_FILE} already exists. Skipping creation.`); 128 | return; 129 | } 130 | 131 | try { 132 | fs.writeFileSync(configPath, JSON.stringify(vimspectorJson, null, 2)); 133 | } catch (e) { 134 | console.error(`Failed to write ${VIMSPECTOR_CONFIG_FILE}`, e); 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/protocol.ts: -------------------------------------------------------------------------------- 1 | import { Range } from 'coc.nvim'; 2 | 3 | export interface IMainClassOption { 4 | readonly mainClass: string; 5 | readonly projectName?: string; 6 | readonly filePath?: string; 7 | } 8 | 9 | export interface IMainMethod extends IMainClassOption { 10 | readonly range: Range; 11 | } 12 | 13 | export type MainMethodResult = Array; 14 | 15 | // See https://github.com/microsoft/vscode-java-debug#options 16 | export interface ISubstitutionVar { 17 | readonly adapterPort: string; 18 | readonly classPaths: string; 19 | readonly mainClass: string; 20 | readonly modulePaths: string; 21 | readonly projectName: string; 22 | } 23 | 24 | export interface IClassPath { 25 | readonly modulePaths: string[]; 26 | readonly classPaths: string[]; 27 | } 28 | -------------------------------------------------------------------------------- /src/resources/vimspector.json: -------------------------------------------------------------------------------- 1 | { 2 | "adapters": { 3 | "coc-java-debug": { 4 | "port": "${AdapterPort}" 5 | } 6 | }, 7 | "configurations": { 8 | "launch": { 9 | "adapter": "coc-java-debug", 10 | "configuration": { 11 | "args": "${args}", 12 | "request": "launch", 13 | "projectName": "${ProjectName}", 14 | "mainClass": "${MainClass}", 15 | "classPaths": ["*${ClassPaths}"] 16 | }, 17 | "breakpoints": { 18 | "exception": { 19 | "caught": "N", 20 | "uncaught": "N" 21 | } 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/settings.ts: -------------------------------------------------------------------------------- 1 | import { commands, workspace } from 'coc.nvim'; 2 | import { substituteFilterVariables } from './classFilter'; 3 | import { Commands } from './commands'; 4 | 5 | export function onConfigurationChange() { 6 | return workspace.onDidChangeConfiguration((params) => { 7 | if (!params.affectsConfiguration('java.debug.settings') && !params.affectsConfiguration('java.debug.logLevel')) { 8 | return; 9 | } 10 | updateDebugSettings(); 11 | }); 12 | } 13 | 14 | export async function updateDebugSettings() { 15 | const debugSettingsRoot = workspace.getConfiguration('java.debug'); 16 | 17 | if (!debugSettingsRoot) { 18 | return; 19 | } 20 | const logLevel = convertLogLevel(debugSettingsRoot.logLevel || ''); 21 | if (debugSettingsRoot.settings && Object.keys(debugSettingsRoot.settings).length) { 22 | try { 23 | const extraSettings = {}; 24 | if (debugSettingsRoot.settings.stepping && Object.keys(debugSettingsRoot.settings.stepping).length) { 25 | const stepFilters = {}; 26 | if (debugSettingsRoot.settings.stepping.skipClasses) { 27 | stepFilters['skipClasses'] = await substituteFilterVariables(debugSettingsRoot.settings.stepping.skipClasses); 28 | } 29 | if (debugSettingsRoot.settings.stepping.skipSynthetics) { 30 | stepFilters['skipSynthetics'] = debugSettingsRoot.settings.stepping.skipSynthetics; 31 | } 32 | if (debugSettingsRoot.settings.stepping.skipStaticInitializers) { 33 | stepFilters['skipStaticInitializers'] = debugSettingsRoot.settings.stepping.skipStaticInitializers; 34 | } 35 | if (debugSettingsRoot.settings.stepping.skipConstructors) { 36 | stepFilters['skipConstructors'] = debugSettingsRoot.settings.stepping.skipConstructors; 37 | } 38 | extraSettings['stepFilters'] = stepFilters; 39 | } 40 | if ( 41 | debugSettingsRoot.settings.exceptionBreakpoint && 42 | Object.keys(debugSettingsRoot.settings.exceptionBreakpoint).length 43 | ) { 44 | const exceptionFilters = {}; 45 | if (debugSettingsRoot.settings.exceptionBreakpoint.exceptionTypes) { 46 | exceptionFilters['exceptionTypes'] = debugSettingsRoot.settings.exceptionBreakpoint.exceptionTypes; 47 | } 48 | if (debugSettingsRoot.settings.exceptionBreakpoint.allowClasses) { 49 | exceptionFilters['allowClasses'] = debugSettingsRoot.settings.exceptionBreakpoint.allowClasses; 50 | } 51 | if (debugSettingsRoot.settings.exceptionBreakpoint.skipClasses) { 52 | exceptionFilters['skipClasses'] = await substituteFilterVariables( 53 | debugSettingsRoot.settings.exceptionBreakpoint.skipClasses, 54 | ); 55 | } 56 | extraSettings['exceptionFilters'] = exceptionFilters; 57 | extraSettings['exceptionFiltersUpdated'] = true; 58 | } 59 | 60 | if (debugSettingsRoot.settings.jdwp) { 61 | if (debugSettingsRoot.settings.jdwp.async) { 62 | extraSettings['asyncJDWP'] = debugSettingsRoot.settings.jdwp.async; 63 | } 64 | if (debugSettingsRoot.settings.jdwp.limitOfVariablesPerJdwpRequest) { 65 | extraSettings['limitOfVariablesPerJdwpRequest'] = Math.max( 66 | debugSettingsRoot.settings.jdwp.limitOfVariablesPerJdwpRequest, 67 | 1, 68 | ); 69 | } 70 | if (debugSettingsRoot.settings.jdwp.requestTimeout) { 71 | extraSettings['requestTimeout'] = Math.max(debugSettingsRoot.settings.jdwp.requestTimeout, 100); 72 | } 73 | } 74 | const settings = await commands.executeCommand( 75 | Commands.JAVA_UPDATE_DEBUG_SETTINGS, 76 | JSON.stringify({ 77 | ...debugSettingsRoot.settings, 78 | ...extraSettings, 79 | logLevel, 80 | }), 81 | ); 82 | console.debug('settings:', settings); 83 | } catch (err) { 84 | // log a warning message and continue, since update settings failure should not block debug session 85 | console.error('Cannot update debug settings.', err); 86 | } 87 | } 88 | } 89 | 90 | function convertLogLevel(commonLogLevel: string) { 91 | // convert common log level to java log level 92 | switch (commonLogLevel.toLowerCase()) { 93 | case 'verbose': 94 | return 'FINE'; 95 | case 'warn': 96 | return 'WARNING'; 97 | case 'error': 98 | return 'SEVERE'; 99 | case 'info': 100 | return 'INFO'; 101 | default: 102 | return 'FINE'; 103 | } 104 | } 105 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es2017", 4 | "lib": ["es2017", "es2018"], 5 | "module": "commonjs", 6 | "declaration": false, 7 | "sourceMap": true, 8 | "outDir": "lib", 9 | "strict": true, 10 | "moduleResolution": "node", 11 | "noImplicitAny": false, 12 | "esModuleInterop": true, 13 | "resolveJsonModule": true 14 | }, 15 | "include": ["src"] 16 | } 17 | -------------------------------------------------------------------------------- /update_server: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -eou pipefail 3 | 4 | # Assumes git@github.com:microsoft/java-debug.git is checked out to a sibling directory. 5 | server_dir="server" 6 | java_debug_dir="../java-debug" 7 | jar_path="${java_debug_dir}/com.microsoft.java.debug.plugin/target" 8 | jar_file="com.microsoft.java.debug.*.jar" 9 | 10 | pushd "${java_debug_dir}" &>/dev/null || exit 1 11 | git pull 12 | ./mvnw clean package 13 | popd &>/dev/null || exit 1 14 | find "${jar_path}" -maxdepth 1 -type f -name "${jar_file}" -exec cp -vf '{}' "${server_dir}" \; 15 | 16 | jars=$(find "${server_dir}" -type f -name "${jar_file}" | sort) 17 | jar_count=$(echo "${jars}" | wc -l) 18 | if [[ "${jar_count}" -lt 2 ]]; then 19 | echo -e "\nThere was no server update. package.json remains the same." 20 | exit 0 21 | fi 22 | 23 | old_jar=$(echo "${jars}" | head -1) 24 | rm "${old_jar}" 25 | new_jar=$(find "${server_dir}" -type f -name "${jar_file}" | tail -1) 26 | escaped_new_jar=$(printf '%s\n' "${new_jar}" | sed -e 's/[\/&]/\\&/g') 27 | sed -i "" \ 28 | -e "s/${server_dir}\/com\.microsoft\.java\.debug\.plugin-[[:digit:]]\{1,2\}\.[[:digit:]]\{1,2\}\.[[:digit:]]\{1,2\}\.jar/${escaped_new_jar}/g" \ 29 | package.json 30 | echo -e "\npackage.json updated to [${new_jar}]" 31 | git add -f "${server_dir}" package.json 32 | --------------------------------------------------------------------------------