<commit_before>package som.langserv.lint;

import java.util.Collection;

import som.langserv.structure.DocumentStructures;


public class LintUseNeedsDefine implements WorkspaceLinter {

  @Override
  public void lint(final Collection<DocumentStructures> structures) {
    // iterate over all references, and make sure there's a matching define
    boolean defined = false;
    if (!defined) {
    }
  }
}
<commit_msg>Implement language-agnostic use-needs-define linter<commit_after>package som.langserv.lint;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DiagnosticSeverity;

import som.langserv.structure.DocumentStructures;
import som.langserv.structure.LanguageElementId;


public class LintUseNeedsDefine implements WorkspaceLinter {

  @Override
  public void lint(final Collection<DocumentStructures> structures) {
    // collect everything that's defined
    Set<LanguageElementId> allDefinedElements = new HashSet<>();
    for (var s : structures) {
      var defs = s.getAllDefinitions();
      if (defs == null) {
        continue;
      }
      allDefinedElements.addAll(defs.keySet());
    }

    // now check everything that's referenced,
    // and report anything for which we do not have a definition
    for (var s : structures) {
      var allRefs = s.getAllReferences();
      if (allRefs == null) {
        continue;
      }

      for (var ref : allRefs.entrySet()) {
        LanguageElementId id = ref.getKey();
        if (!allDefinedElements.contains(id)) {
          for (var r : ref.getValue()) {
            Diagnostic diag = new Diagnostic();
            diag.setRange(r.getRange());
            diag.setMessage("`" + id.getName()
                + "` does not seem to be defined, which might cause run-time errors.");
            diag.setSeverity(DiagnosticSeverity.Warning);
            s.addDiagnostic(diag);
          }
        }
      }
    }
  }
}

{'fraction_non_alphanumeric': 0.10161779575328615, 'fraction_numerical': 0.0010111223458038423, 'mean_word_length': 2.9188118811881187, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7654949', 'n_tokens_mistral': 586, 'n_tokens_neox': 565, 'n_words': 143}