You are on page 1of 20

Digging into

solargraph
Autocomplete, linting and
debugging with new ruby
tools

Copyright 2022 AppTweak S.A. All Rights Reserved


Building a stronger Ruby community.Together.
Ruby Belgium is a community of programming enthusiasts using ruby as a tool to connect, build
and share knowledge.

We promote the use of the ruby language, support the ruby job market, organize events to help
beginners through their learning journey and gather people around our core values: sharing, fun,
mutual respect.

We believe ruby has a big role to play in the Belgian tech ecosystem. Ruby is also a good starting
point for anyone who wants to learn to program.
#1 ASO Tool
for Apps & Games
Driven by Data Science

AppTweak fuels growth for the world’s most


popular apps and games by providing
actionable insights in a simple interface.

Copyright 2022 AppTweak S.A. All Rights Reserved


Recognized ASO Leader
Trusted by 1,800+ mobile leaders in 70+ countries.

Copyright 2022 AppTweak S.A. All Rights Reserved


About our Dev Team
Today's topics

- Solargraph: What is it? How to make it work better for your project

- Solargraph + Rubocop: Making both work together, auto-correct from your editor

- debug + rdbg: Using remote debugging through vscode or the terminal

Copyright 2022 AppTweak S.A. All Rights Reserved


Solargraph: What is it?

From their website:

Solargraph is a Ruby gem that provides intellisense features through Microsoft's language server protocol.

Solargraph works with any editor that has a way to plug the LSP: VSCode, NeoVim, Emacs, etc

Copyright 2022 AppTweak S.A. All Rights Reserved


Solargraph: Basic setup

The basic setup here is to first install the solargraph gem:


gem install solargraph

You can of course bundle it too.

Then, you need to use it in your editor, for VSCode that means installing the Ruby Solargraph extension by Castwide.

Copyright 2022 AppTweak S.A. All Rights Reserved


Solargraph: improving autocomplete

When you install Solargraph it is not great™.

It will index files on your project, but if you're using rails the out-of-the-box autocomplete is not that good either.

We can improve it with three steps


- You should make sure that yard docs are generated for each new gem installed, with yard config
--gem-install-yri
- You need to generate yard docs for all your already-installed gems, with yard gems

- If you use rails, you need to add the solargraph-rails plugin. It is a collection of annotations to help Solargraph
find its way around all the meta-programming.

Copyright 2022 AppTweak S.A. All Rights Reserved


Solargraph: before and after

Copyright 2022 AppTweak S.A. All Rights Reserved


Solargraph: Pitfalls on config

Depending on the editor you're using, you might have some issues around having solargraph bundled/not bundled.

What if you need to troubleshoot it?


solargraph scan -v

Solargraph, as most static analysis in Ruby, really has issues with meta-programming and DSLs, but you can improve it
with plugins, like the solargraph-rails, or by adding annotations:

# @!parse
# class ActiveRecord::Base
# extend ActiveRecord::QueryMethods
# extend ActiveRecord::FinderMethods
# extend ActiveRecord::Associations::ClassMethods
# include ActiveRecord::Persistence
# end

Lastly, files with syntax errors will show no autocomplete at all.

Copyright 2022 AppTweak S.A. All Rights Reserved


Rubocop and other tools

While rubocop is probably the most known linter for ruby, standard is being adopted by some projects to reduce bike
shedding around the rules.

rubyfmt has been making rounds as a fast formatter (it is partly written in rust), intended to be used at file save time.
Developed by stripe and being used internally.

Copyright 2022 AppTweak S.A. All Rights Reserved


Solargraph + Rubocop: Lint tips on your editor

If your solargraph setup is working as expected, you can also enable one extra thing: diagnostics.

It will use reporters from .solargraph.yml, if it exists.

To see available reporters use solargraph reporters

Copyright 2022 AppTweak S.A. All Rights Reserved


Solargraph + Rubocop: Auto correct files

Enabling "formatting" in the Solargraph options allows you to use the "format document" option from vscode based on
rubocop auto-correct.

Copyright 2022 AppTweak S.A. All Rights Reserved


Debug + rdbg: What is debug?

This library provides debugging functionality to Ruby (MRI) 2.6 and later.

This debug.rb is replacement of traditional lib/debug.rb standard library which is implemented by set_trace_func. New
debug.rb has several advantages:

Fast: No performance penalty on non-stepping mode and non-breakpoints.

Remote debugging: Support remote debugging natively.

More in https://github.com/ruby/debug

Copyright 2022 AppTweak S.A. All Rights Reserved


Debug + rdbg: Replacing binding.pry

Instead of adding debugging with binding.pry, the new method of debugging is using debugger.

Most commands (up, down, backtrace, etc) are similar, regular usage should not require a lot of adaptation.

Copyright 2022 AppTweak S.A. All Rights Reserved


Debug + rdbg: Cons of debug?

Current version of debug is quite recent (May 2021) and it still has sub-par support for a few cases. Multi-thread mostly
works, but seems to break in random points.

There's still no support for ractors.

Copyright 2022 AppTweak S.A. All Rights Reserved


Debug + rdbg: Setup for vscode

After adding debug to your Gemfile instead of binding.pry.

You need to install the VSCode rdbg Ruby Debugger extension. Then, for each project you need a launch.json. vscode
will suggest a default one if you don't have one.

Copyright 2022 AppTweak S.A. All Rights Reserved


Thank you!

Copyright 2022 AppTweak S.A. All Rights Reserved

You might also like