summaryrefslogtreecommitdiff
path: root/lib/gitlab/marginalia/inline_annotation.rb
blob: 6d78f3b81ec4deadba492a2cf7eb0923e2b7d8a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

# Module with util methods to support ::Marginalia.without_annotation method.

module Gitlab
  module Marginalia
    module InlineAnnotation
      def without_annotation(&block)
        return unless block.present?

        annotation_stack.push(false)
        yield
      ensure
        annotation_stack.pop
      end

      def with_annotation(comment, &block)
        annotation_stack.push(true)
        super(comment, &block)
      ensure
        annotation_stack.pop
      end

      def annotation_stack
        Thread.current[:annotation_stack] ||= []
      end

      def annotation_stack_top
        annotation_stack.last
      end

      def annotation_allowed?
        annotation_stack.empty? ? true : annotation_stack_top
      end
    end
  end
end