@@ -350,7 +350,9 @@ function double_to_triple_equal(x, _, conn)
350350end
351351
352352function get_spdx_header (doc:: Document )
353- m = match (r" (*ANYCRLF)^# SPDX-License-Identifier:\h +((?:[\w\. -]+)(?:\h +[\w\. -]+)*)\h *$" , get_text (doc))
353+ # note the multiline flag - without that, we'd try to match the end of the _document_
354+ # instead of the end of the line.
355+ m = match (r" (*ANYCRLF)^# SPDX-License-Identifier:\h +((?:[\w\. -]+)(?:\h +[\w\. -]+)*)\h *$" m , get_text (doc))
354356 return m === nothing ? m : String (m[1 ])
355357end
356358
@@ -377,7 +379,11 @@ function identify_short_identifier(server::LanguageServerInstance, file::Documen
377379 end
378380 if length (candidate_identifiers) == 1
379381 return first (candidate_identifiers)
382+ else
383+ numerous = iszero (length (candidate_identifiers)) ? " no" : " multiple"
384+ @warn " Found $numerous candidates for the SPDX header from open files, falling back to LICENSE" Candidates= candidate_identifiers
380385 end
386+
381387 # Fallback to looking for a license file in the same workspace folder
382388 candidate_files = String[]
383389 for dir in server. workspaceFolders
@@ -387,21 +393,36 @@ function identify_short_identifier(server::LanguageServerInstance, file::Documen
387393 end
388394 end
389395 end
390- length (candidate_files) == 1 || return nothing
391- license = read (first (candidate_files), String)
396+
397+ num_candidates = length (candidate_files)
398+ if num_candidates != 1
399+ iszero (num_candidates) && @warn " No candidate for licenses found, can't add identifier!"
400+ num_candidates > 1 && @warn " More than one candidate for licenses found, choose licensing manually!"
401+ return nothing
402+ end
392403
393404 # This is just a heuristic, but should be OK since this is not something automated, and
394405 # the programmer will see directly if the wrong license is added.
395- # TODO : Add more licenses...
396- if contains (license, r" MIT\s +(\" ?Expat\" ?\s +)?License" )
406+ license_text = read (first (candidate_files), String)
407+
408+ # Some known different spellings of some licences
409+ if contains (license_text, r" ^\s *MIT\s +(\" ?Expat\" ?\s +)?Licen[sc]e" )
397410 return " MIT"
411+ elseif contains (license_text, r" ^\s *EUROPEAN\s +UNION\s +PUBLIC\s +LICEN[CS]E\s +v\. " i )
412+ # the first version should be the EUPL version
413+ version = match (r" \d\.\d " , license_text). match
414+ return " EUPL-$version "
398415 end
416+
417+ @warn " A license was found, but could not be identified! Consider adding its licence identifier once to a file manually so that LanguageServer.jl can find it automatically next time." Location= first (candidate_files)
399418 return nothing
400419end
401420
402421function add_license_header (x, server:: LanguageServerInstance , conn)
403422 file, _ = get_file_loc (x)
423+ # does the current file already have a header?
404424 get_spdx_header (file) === nothing || return # TODO : Would be nice to check this already before offering the action
425+ # no, so try to find one
405426 short_identifier = identify_short_identifier (server, file)
406427 short_identifier === nothing && return
407428 tde = TextDocumentEdit (VersionedTextDocumentIdentifier (get_uri (file), get_version (file)), TextEdit[
0 commit comments